Whamcloud - gitweb
- fixes to osc_brw path to address fsx failures. We in fact uncovered
[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/smp_lock.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 /*
37  * Remove page from dirty list
38  */
39 static void __set_page_clean(struct page *page)
40 {
41         struct address_space *mapping = page->mapping;
42         struct inode *inode;
43
44         if (!mapping)
45                 return;
46
47 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
48         spin_lock(&pagecache_lock);
49 #endif
50
51         list_del(&page->list);
52         list_add(&page->list, &mapping->clean_pages);
53
54         inode = mapping->host;
55         if (list_empty(&mapping->dirty_pages)) {
56                 CDEBUG(D_INODE, "inode clean\n");
57                 inode->i_state &= ~I_DIRTY_PAGES;
58         }
59 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
60         spin_unlock(&pagecache_lock);
61 #endif
62         EXIT;
63 }
64
65 inline void set_page_clean(struct page *page)
66 {
67         if (PageDirty(page)) {
68                 ClearPageDirty(page);
69                 __set_page_clean(page);
70         }
71 }
72
73 /* SYNCHRONOUS I/O to object storage for an inode */
74 static int ll_brw(int cmd, struct inode *inode, struct page *page, int create)
75 {
76         struct ll_inode_info *lli = ll_i2info(inode);
77         struct lov_stripe_md *lsm = lli->lli_smd;
78         struct io_cb_data *cbd = ll_init_cb();
79         struct brw_page pg;
80         int err;
81         ENTRY;
82
83         CHECK_MOUNT_EPOCH(inode);
84
85         if (!cbd)
86                 RETURN(-ENOMEM);
87
88         pg.pg = page;
89         pg.off = ((obd_off)page->index) << PAGE_SHIFT;
90
91         if (cmd == OBD_BRW_WRITE && (pg.off + PAGE_SIZE > inode->i_size))
92                 pg.count = inode->i_size % PAGE_SIZE; 
93         else
94                 pg.count = PAGE_SIZE;
95
96         pg.flag = create ? OBD_BRW_CREATE : 0;
97
98         err = obd_brw(cmd, ll_i2obdconn(inode),lsm, 1, &pg, ll_sync_io_cb, cbd);
99
100         RETURN(err);
101 } /* ll_brw */
102
103 /* returns the page unlocked, but with a reference */
104 static int ll_readpage(struct file *file, struct page *page)
105 {
106         struct inode *inode = page->mapping->host;
107         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
108         int rc = 0;
109         ENTRY;
110
111         if (!PageLocked(page))
112                 LBUG();
113
114         if (inode->i_size <= offset) {
115                 memset(kmap(page), 0, PAGE_SIZE);
116                 kunmap(page);
117                 GOTO(readpage_out, rc);
118         }
119
120         if (PageUptodate(page)) {
121                 CERROR("Explain this please?\n");
122                 GOTO(readpage_out, rc);
123         }
124
125         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
126         EXIT;
127
128  readpage_out:
129         if (!rc)
130                 SetPageUptodate(page);
131         unlock_page(page);
132         return 0;
133 } /* ll_readpage */
134
135 void ll_truncate(struct inode *inode)
136 {
137         struct obdo oa = {0};
138         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
139         struct lustre_handle *lockhs = NULL;
140         int err;
141         ENTRY;
142
143         if (!lsm) {
144                 /* object not yet allocated */
145                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
146                 return;
147         }
148
149         oa.o_id = lsm->lsm_object_id;
150         oa.o_mode = inode->i_mode;
151         oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
152
153         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after "LPD64")\n",
154                oa.o_id, inode->i_size);
155
156         err = ll_size_lock(inode, lsm, inode->i_size, LCK_PW, &lockhs);
157         if (err) {
158                 CERROR("ll_size_lock failed: %d\n", err);
159                 /* FIXME: What to do here?  It's too late to back out... */
160                 LBUG();
161         }
162
163         /* truncate == punch from new size to absolute end of file */
164         err = obd_punch(ll_i2obdconn(inode), &oa, lsm, inode->i_size,
165                         OBD_OBJECT_EOF);
166         if (err)
167                 CERROR("obd_truncate fails (%d)\n", err);
168         else
169                 obdo_to_inode(inode, &oa, oa.o_valid);
170
171         err = ll_size_unlock(inode, lsm, LCK_PW, lockhs);
172         if (err)
173                 CERROR("ll_size_unlock failed: %d\n", err);
174
175         EXIT;
176         return;
177 } /* ll_truncate */
178
179 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
180
181 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
182                             unsigned to)
183 {
184         struct inode *inode = page->mapping->host;
185         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
186         int rc = 0;
187         char *addr;
188         ENTRY;
189
190         addr = kmap(page);
191         if (!PageLocked(page))
192                 LBUG();
193
194         if (Page_Uptodate(page))
195                 GOTO(prepare_done, rc);
196
197         /* We're completely overwriting an existing page, so _don't_ set it up
198          * to date until commit_write */
199         if (from == 0 && to == PAGE_SIZE)
200                 RETURN(0);
201
202         /* We are writing to a new page, no need to read old data */
203         if (inode->i_size <= offset) {
204                 memset(addr, 0, PAGE_SIZE);
205                 GOTO(prepare_done, rc=0);
206         }
207
208         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
209
210         EXIT;
211  prepare_done:
212         if (!rc)
213                 SetPageUptodate(page);
214
215         return rc;
216 }
217
218 /* returns the page unlocked, but with a reference */
219 static int ll_writepage(struct page *page)
220 {
221         struct inode *inode = page->mapping->host;
222         int err;
223         ENTRY;
224
225         if (!PageLocked(page))
226                 LBUG();
227
228         err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
229         if ( !err ) {
230                 //SetPageUptodate(page);
231                 set_page_clean(page);
232         } else {
233                 CERROR("ll_brw failure %d\n", err);
234         }
235         unlock_page(page);
236         RETURN(err);
237 }
238
239
240 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
241  * too */
242 static int ll_commit_write(struct file *file, struct page *page,
243                            unsigned from, unsigned to)
244 {
245         int create = 1;
246         struct inode *inode = page->mapping->host;
247         struct ll_inode_info *lli = ll_i2info(inode);
248         struct lov_stripe_md *md = lli->lli_smd;
249         struct brw_page pg;
250         int err;
251         loff_t size;
252         struct io_cb_data *cbd = ll_init_cb();
253         ENTRY;
254
255         CHECK_MOUNT_EPOCH(inode);
256
257         pg.pg = page;
258         pg.count = to;
259         pg.off = (((obd_off)page->index) << PAGE_SHIFT);
260         pg.flag = create ? OBD_BRW_CREATE : 0;
261
262         if (!cbd)
263                 RETURN(-ENOMEM);
264
265         SetPageUptodate(page);
266
267         if (!PageLocked(page))
268                 LBUG();
269
270         CDEBUG(D_INODE, "commit_page writing (off "LPD64"), count "LPD64"\n",
271                pg.off, pg.count);
272
273         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md,
274                       1, &pg, ll_sync_io_cb, cbd);
275         kunmap(page);
276
277         size = pg.off + pg.count;
278         /* do NOT truncate when writing in the middle of a file */
279         if (size > inode->i_size)
280                 inode->i_size = size;
281
282         RETURN(err);
283 } /* ll_commit_write */
284
285
286 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
287                         unsigned long blocknr, int blocksize)
288 {
289         obd_count bufs_per_obdo = iobuf->nr_pages;
290         struct ll_inode_info *lli = ll_i2info(inode);
291         struct lov_stripe_md *lsm = lli->lli_smd;
292         struct brw_page *pga;
293         int i, rc = 0;
294         struct io_cb_data *cbd;
295
296         CHECK_MOUNT_EPOCH(inode);
297
298         ENTRY;
299         if (!lsm || !lsm->lsm_object_id)
300                 RETURN(-ENOMEM);
301
302         if (blocksize != PAGE_SIZE) {
303                 CERROR("direct_IO blocksize != PAGE_SIZE\n");
304                 RETURN(-EINVAL);
305         }
306
307         cbd = ll_init_cb();
308         if (!cbd)
309                 RETURN(-ENOMEM);
310
311         OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
312         if (!pga) {
313                 OBD_FREE(cbd, sizeof(*cbd));
314                 RETURN(-ENOMEM);
315         }
316
317         /* NB: we can't use iobuf->maplist[i]->index for the offset
318          * instead of "blocknr" because ->index contains garbage.
319          */
320         for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
321                 pga[i].pg = iobuf->maplist[i];
322                 pga[i].count = PAGE_SIZE;
323                 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
324                 pga[i].flag = OBD_BRW_CREATE;
325         }
326
327         rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
328                      ll_i2obdconn(inode), lsm, bufs_per_obdo, pga,
329                      ll_sync_io_cb, cbd);
330         if (rc == 0)
331                 rc = bufs_per_obdo * PAGE_SIZE;
332
333         OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
334         RETURN(rc);
335 }
336
337 int ll_flush_inode_pages(struct inode * inode)
338 {
339         obd_count        bufs_per_obdo = 0;
340         obd_size         *count = NULL;
341         obd_off          *offset = NULL;
342         obd_flag         *flags = NULL;
343         int              err = 0;
344
345         ENTRY;
346
347         spin_lock(&pagecache_lock);
348
349         spin_unlock(&pagecache_lock);
350
351
352         OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
353         OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
354         OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
355         if (!count || !offset || !flags)
356                 GOTO(out, err=-ENOMEM);
357
358 #if 0
359         for (i = 0 ; i < bufs_per_obdo ; i++) {
360                 count[i] = PAGE_SIZE;
361                 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
362                 flags[i] = OBD_BRW_CREATE;
363         }
364
365         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
366                       ll_i2info(inode)->lli_smd, bufs_per_obdo,
367                       iobuf->maplist, count, offset, flags, NULL, NULL);
368         if (err == 0)
369                 err = bufs_per_obdo * 4096;
370 #endif
371  out:
372         OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
373         OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
374         OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
375         RETURN(err);
376 }
377
378 #endif
379
380
381 struct address_space_operations ll_aops = {
382         readpage: ll_readpage,
383 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
384         direct_IO: ll_direct_IO,
385         writepage: ll_writepage,
386         sync_page: block_sync_page,
387         prepare_write: ll_prepare_write,
388         commit_write: ll_commit_write,
389         bmap: NULL
390 #endif
391 };