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