ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / linux / nfs_page.h
1 /*
2  * linux/include/linux/nfs_page.h
3  *
4  * Copyright (C) 2000 Trond Myklebust
5  *
6  * NFS page cache wrapper.
7  */
8
9 #ifndef _LINUX_NFS_PAGE_H
10 #define _LINUX_NFS_PAGE_H
11
12
13 #include <linux/list.h>
14 #include <linux/pagemap.h>
15 #include <linux/wait.h>
16 #include <linux/nfs_fs_sb.h>
17 #include <linux/sunrpc/auth.h>
18 #include <linux/nfs_xdr.h>
19
20 #include <asm/atomic.h>
21
22 /*
23  * Valid flags for a dirty buffer
24  */
25 #define PG_BUSY                 0
26 #define PG_NEED_COMMIT          1
27 #define PG_NEED_RESCHED         2
28
29 struct nfs_page {
30         struct list_head        wb_list,        /* Defines state of page: */
31                                 *wb_list_head;  /*      read/write/commit */
32         struct file             *wb_file;
33         fl_owner_t              wb_lockowner;
34         struct inode            *wb_inode;
35         struct rpc_cred         *wb_cred;
36         struct nfs4_state       *wb_state;
37         struct page             *wb_page;       /* page to read in/write out */
38         atomic_t                wb_complete;    /* i/os we're waiting for */
39         wait_queue_head_t       wb_wait;        /* wait queue */
40         unsigned long           wb_index;       /* Offset >> PAGE_CACHE_SHIFT */
41         unsigned int            wb_offset,      /* Offset & ~PAGE_CACHE_MASK */
42                                 wb_pgbase,      /* Start of page data */
43                                 wb_bytes,       /* Length of request */
44                                 wb_count;       /* reference count */
45         unsigned long           wb_flags;
46         struct nfs_writeverf    wb_verf;        /* Commit cookie */
47 };
48
49 #define NFS_WBACK_BUSY(req)     (test_bit(PG_BUSY,&(req)->wb_flags))
50 #define NFS_NEED_COMMIT(req)    (test_bit(PG_NEED_COMMIT,&(req)->wb_flags))
51 #define NFS_NEED_RESCHED(req)   (test_bit(PG_NEED_RESCHED,&(req)->wb_flags))
52
53 extern  struct nfs_page *nfs_create_request(struct file *, struct inode *,
54                                             struct page *,
55                                             unsigned int, unsigned int);
56 extern  void nfs_clear_request(struct nfs_page *req);
57 extern  void nfs_release_request(struct nfs_page *req);
58
59
60 extern  void nfs_list_add_request(struct nfs_page *, struct list_head *);
61
62 extern  int nfs_scan_list(struct list_head *, struct list_head *,
63                           unsigned long, unsigned int);
64 extern  int nfs_coalesce_requests(struct list_head *, struct list_head *,
65                                   unsigned int);
66 extern  int nfs_wait_on_request(struct nfs_page *);
67
68 extern  spinlock_t nfs_wreq_lock;
69
70 /*
71  * Lock the page of an asynchronous request without incrementing the wb_count
72  */
73 static inline int
74 nfs_lock_request_dontget(struct nfs_page *req)
75 {
76         if (test_and_set_bit(PG_BUSY, &req->wb_flags))
77                 return 0;
78         return 1;
79 }
80
81 /*
82  * Lock the page of an asynchronous request
83  */
84 static inline int
85 nfs_lock_request(struct nfs_page *req)
86 {
87         if (test_and_set_bit(PG_BUSY, &req->wb_flags))
88                 return 0;
89         req->wb_count++;
90         return 1;
91 }
92
93 static inline void
94 nfs_unlock_request(struct nfs_page *req)
95 {
96         if (!NFS_WBACK_BUSY(req)) {
97                 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
98                 BUG();
99         }
100         smp_mb__before_clear_bit();
101         clear_bit(PG_BUSY, &req->wb_flags);
102         smp_mb__after_clear_bit();
103         wake_up_all(&req->wb_wait);
104         nfs_release_request(req);
105 }
106
107 /**
108  * nfs_list_remove_request - Remove a request from its wb_list
109  * @req: request
110  */
111 static inline void
112 nfs_list_remove_request(struct nfs_page *req)
113 {
114         if (list_empty(&req->wb_list))
115                 return;
116         if (!NFS_WBACK_BUSY(req)) {
117                 printk(KERN_ERR "NFS: unlocked request attempted removed from list!\n");
118                 BUG();
119         }
120         list_del_init(&req->wb_list);
121         req->wb_list_head = NULL;
122 }
123
124 static inline int
125 nfs_defer_commit(struct nfs_page *req)
126 {
127         if (test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags))
128                 return 0;
129         return 1;
130 }
131
132 static inline void
133 nfs_clear_commit(struct nfs_page *req)
134 {
135         smp_mb__before_clear_bit();
136         clear_bit(PG_NEED_COMMIT, &req->wb_flags);
137         smp_mb__after_clear_bit();
138 }
139
140 static inline int
141 nfs_defer_reschedule(struct nfs_page *req)
142 {
143         if (test_and_set_bit(PG_NEED_RESCHED, &req->wb_flags))
144                 return 0;
145         return 1;
146 }
147
148 static inline void
149 nfs_clear_reschedule(struct nfs_page *req)
150 {
151         smp_mb__before_clear_bit();
152         clear_bit(PG_NEED_RESCHED, &req->wb_flags);
153         smp_mb__after_clear_bit();
154 }
155
156 static inline struct nfs_page *
157 nfs_list_entry(struct list_head *head)
158 {
159         return list_entry(head, struct nfs_page, wb_list);
160 }
161
162 static inline
163 loff_t req_offset(struct nfs_page *req)
164 {
165         return (((loff_t)req->wb_index) << PAGE_CACHE_SHIFT) + req->wb_offset;
166 }
167
168 #endif /* _LINUX_NFS_PAGE_H */