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