Whamcloud - gitweb
eb5a602698b4d177f7d4df5320f293200290b9fd
[fs/lustre-release.git] / lustre / llite / rw.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Lite I/O Page Cache
5  *
6  * Copyright (C) 2002 Cluster File Systems, Inc. 
7  */
8
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/string.h>
13 #include <linux/stat.h>
14 #include <linux/iobuf.h>
15 #include <linux/errno.h>
16 #include <linux/locks.h>
17 #include <linux/unistd.h>
18 #include <linux/version.h>
19 #include <asm/system.h>
20 #include <asm/uaccess.h>
21
22 #include <linux/fs.h>
23 #include <linux/stat.h>
24 #include <asm/uaccess.h>
25 #include <asm/segment.h>
26 #include <linux/mm.h>
27 #include <linux/pagemap.h>
28 #include <linux/smp_lock.h>
29
30 #define DEBUG_SUBSYSTEM S_LLITE
31
32 #include <linux/lustre_mds.h>
33 #include <linux/lustre_lite.h>
34 #include <linux/lustre_lib.h>
35
36 /* SYNCHRONOUS I/O to object storage for an inode */
37 static int ll_brw(int rw, struct inode *inode, struct page *page, int create)
38 {
39         obd_count        num_obdo = 1;
40         obd_count        bufs_per_obdo = 1;
41         struct obdo     *oa;
42         obd_size         count = PAGE_SIZE;
43         obd_off          offset = ((obd_off)page->index) << PAGE_SHIFT;
44         obd_flag         flags = create ? OBD_BRW_CREATE : 0;
45         int              err;
46         ENTRY;
47
48         oa = ll_i2info(inode)->lli_obdo;
49         err = obd_brw(rw, ll_i2obdconn(inode), num_obdo, &oa, &bufs_per_obdo,
50                       &page, &count, &offset, &flags, NULL);
51         RETURN(err);
52 } /* ll_brw */
53
54 /* returns the page unlocked, but with a reference */
55 static int ll_readpage(struct file *file, struct page *page)
56 {
57         struct inode *inode = page->mapping->host;
58         int rc = 0;
59         ENTRY;
60
61         if (!PageLocked(page))
62                 LBUG();
63
64         if (((inode->i_size + PAGE_CACHE_SIZE -1)>>PAGE_SHIFT) <= page->index) {
65                 memset(kmap(page), 0, PAGE_CACHE_SIZE);
66                 kunmap(page);
67                 GOTO(readpage_out, rc);
68         }
69
70         if (Page_Uptodate(page)) {
71                 CERROR("Explain this please?\n");
72                 GOTO(readpage_out, rc);
73         }
74
75         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
76         EXIT;
77
78  readpage_out:
79         if (!rc)
80                 SetPageUptodate(page);
81         UnlockPage(page);
82         return 0;
83 } /* ll_readpage */
84
85
86 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
87                             unsigned to)
88 {
89         struct inode *inode = page->mapping->host;
90         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
91         int rc = 0;
92         char *addr;
93         ENTRY; 
94         
95         addr = kmap(page);
96         if (!PageLocked(page))
97                 LBUG();
98
99         if (Page_Uptodate(page))
100                 GOTO(prepare_done, rc);
101
102         if (offset + from >= inode->i_size) {
103                 memset(addr, 0, PAGE_SIZE);
104                 GOTO(prepare_done, rc);
105         }
106
107         /* We're completely overwriting an existing page, so _don't_ set it up
108          * to date until commit_write */
109         if (from == 0 && to == PAGE_SIZE) {
110                 memset(addr, 0, PAGE_SIZE);
111                 RETURN(0);
112         }
113
114         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
115
116         EXIT;
117  prepare_done:
118         if (!rc)
119                 SetPageUptodate(page);
120
121         return rc;
122 }
123
124 /* returns the page unlocked, but with a reference */
125 static int ll_writepage(struct page *page)
126 {
127         struct inode *inode = page->mapping->host;
128         int err;
129         ENTRY;
130
131         if (!PageLocked(page))
132                 LBUG();
133
134         err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
135         if ( !err ) {
136                 //SetPageUptodate(page);
137                 set_page_clean(page);
138         } else {
139                 CERROR("ll_brw failure %d\n", err);
140         }
141         UnlockPage(page); 
142         RETURN(err);
143 }
144
145 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
146  * too */
147 static int ll_commit_write(struct file *file, struct page *page,
148                            unsigned from, unsigned to)
149 {
150         int create = 1;
151         struct inode *inode = page->mapping->host;
152         obd_count        num_obdo = 1;
153         obd_count        bufs_per_obdo = 1;
154         struct obdo     *oa;
155         obd_size         count = to;
156         obd_off          offset = (((obd_off)page->index) << PAGE_SHIFT);
157         obd_flag         flags = create ? OBD_BRW_CREATE : 0;
158         int              err;
159         struct iattr     iattr;
160
161         ENTRY;
162         oa = ll_i2info(inode)->lli_obdo;
163
164         SetPageUptodate(page);
165
166         if (!PageLocked(page))
167                 LBUG();
168
169         CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n", 
170                from, to, (unsigned long long)count);
171
172         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), num_obdo, &oa,
173                       &bufs_per_obdo, &page, &count, &offset, &flags, NULL);
174         kunmap(page);
175
176         iattr.ia_size = offset + to;
177         if (iattr.ia_size > inode->i_size) {
178                 /* do NOT truncate when writing in the middle of a file */
179                 inode->i_size = iattr.ia_size;
180                 iattr.ia_valid = ATTR_SIZE;
181 #if 0
182                 err = ll_inode_setattr(inode, &iattr, 0);
183                 if (err) {
184                         CERROR("failed - %d.\n", err);
185                         err = -EIO;
186                 }
187 #endif
188         }
189
190         RETURN(err);
191 } /* ll_commit_write */
192
193 void ll_truncate(struct inode *inode)
194 {
195         struct obdo *oa;
196         int err;
197         ENTRY;
198
199         oa = ll_i2info(inode)->lli_obdo;
200         
201         CDEBUG(D_INFO, "calling punch for %ld (%Lu bytes at 0)\n",
202                (long)oa->o_id, (unsigned long long)oa->o_size);
203         err = obd_punch(ll_i2obdconn(inode), oa, oa->o_size, 0);
204
205         if (err) {
206                 CERROR("obd_truncate fails (%d)\n", err);
207         }
208         EXIT;
209         return;
210 } /* ll_truncate */
211
212 int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
213                  unsigned long blocknr, int blocksize)
214 {
215         int i;
216         obd_count        num_obdo = 1;
217         obd_count        bufs_per_obdo = iobuf->nr_pages;
218         struct obdo     *oa = NULL;
219         obd_size         *count = NULL;
220         obd_off          *offset = NULL;
221         obd_flag         *flags = NULL;
222         int              rc = 0;
223
224         ENTRY;
225
226         if (blocksize != PAGE_SIZE) {
227                 CERROR("direct_IO blocksize != PAGE_SIZE, what to do?\n");
228                 LBUG();
229         }
230
231         OBD_ALLOC(count, sizeof(obd_size) * bufs_per_obdo);
232         OBD_ALLOC(offset, sizeof(obd_off) * bufs_per_obdo);
233         OBD_ALLOC(flags, sizeof(obd_flag) * bufs_per_obdo);
234         if (!count || !offset || !flags)
235                 GOTO(out, rc = -ENOMEM);
236
237         /* NB: we can't use iobuf->maplist[i]->index for the offset
238          * instead of "blocknr" because ->index contains garbage.
239          */
240         for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
241                 count[i] = PAGE_SIZE;
242                 offset[i] = (obd_off)blocknr << PAGE_SHIFT;
243                 flags[i] = OBD_BRW_CREATE;
244         }
245
246         oa = ll_i2info(inode)->lli_obdo;
247         if (!oa)
248                 GOTO(out, rc = -ENOMEM);
249         rc = obd_brw(rw, ll_i2obdconn(inode), num_obdo, &oa, &bufs_per_obdo,
250                       iobuf->maplist, count, offset, flags, NULL);
251         if (rc == 0) 
252                 rc = bufs_per_obdo * PAGE_SIZE;
253
254  out:
255         if (flags) 
256                 OBD_FREE(flags, sizeof(obd_flag) * bufs_per_obdo); 
257         if (count) 
258                 OBD_FREE(count, sizeof(obd_count) * bufs_per_obdo); 
259         if (offset) 
260                 OBD_FREE(offset, sizeof(obd_off) * bufs_per_obdo); 
261         RETURN(rc);
262 }
263
264
265 int ll_flush_inode_pages(struct inode * inode)
266 {
267         //int i;
268         //        obd_count        num_obdo = 1;
269         obd_count        bufs_per_obdo = 0;
270         struct obdo     *oa = NULL;
271         obd_size         *count = NULL;
272         obd_off          *offset = NULL;
273         obd_flag         *flags = NULL;
274         int              err = 0;
275
276         ENTRY;
277
278         spin_lock(&pagecache_lock);
279
280         spin_unlock(&pagecache_lock);
281
282
283         OBD_ALLOC(count, sizeof(obd_size) * bufs_per_obdo); 
284         if (!count)
285                 GOTO(out, err=-ENOMEM); 
286
287         OBD_ALLOC(offset, sizeof(obd_off) * bufs_per_obdo); 
288         if (!offset)
289                 GOTO(out, err=-ENOMEM); 
290
291         OBD_ALLOC(flags, sizeof(obd_flag) * bufs_per_obdo); 
292         if (!flags)
293                 GOTO(out, err=-ENOMEM); 
294
295 #if 0
296         for (i = 0 ; i < bufs_per_obdo ; i++) { 
297                 count[i] = PAGE_SIZE;
298                 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
299                 flags[i] = OBD_BRW_CREATE;
300         }
301
302         oa = ll_oa_from_inode(inode, OBD_MD_FLNOTOBD);
303         if (!oa)
304                 RETURN(-ENOMEM);
305
306         err = obd_brw(rw, ll_i2obdconn(inode), num_obdo, &oa, &bufs_per_obdo,
307                       iobuf->maplist, count, offset, flags);
308         if (err == 0) 
309                 err = bufs_per_obdo * 4096;
310 #endif
311  out:
312         if (oa) 
313                 obdo_free(oa);
314         if (flags) 
315                 OBD_FREE(flags, sizeof(obd_flag) * bufs_per_obdo); 
316         if (count) 
317                 OBD_FREE(count, sizeof(obd_count) * bufs_per_obdo); 
318         if (offset) 
319                 OBD_FREE(offset, sizeof(obd_off) * bufs_per_obdo); 
320         RETURN(err);
321 }
322
323
324
325 struct address_space_operations ll_aops = {
326         readpage: ll_readpage,
327         writepage: ll_writepage,
328 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
329         direct_IO: ll_direct_IO,
330 #endif
331         sync_page: block_sync_page,
332         prepare_write: ll_prepare_write, 
333         commit_write: ll_commit_write,
334         bmap: NULL
335 };