Whamcloud - gitweb
b=596505
[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         /* prepare write should not read what lies beyond the end of
121            the file */
122         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
123
124         EXIT;
125  prepare_done:
126         if (!rc)
127                 SetPageUptodate(page);
128
129         return rc;
130 }
131
132 /* returns the page unlocked, but with a reference */
133 static int ll_writepage(struct page *page)
134 {
135         struct inode *inode = page->mapping->host;
136         int err;
137         ENTRY;
138
139         if (!PageLocked(page))
140                 LBUG();
141
142         err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
143         if ( !err ) {
144                 //SetPageUptodate(page);
145                 set_page_clean(page);
146         } else {
147                 CERROR("ll_brw failure %d\n", err);
148         }
149         UnlockPage(page);
150         RETURN(err);
151 }
152
153
154 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
155  * too */
156 static int ll_commit_write(struct file *file, struct page *page,
157                            unsigned from, unsigned to)
158 {
159         int create = 1;
160         struct inode *inode = page->mapping->host;
161         struct ll_inode_info *lli = ll_i2info(inode);
162         struct lov_stripe_md *md = lli->lli_smd;
163         struct brw_page pg;
164         int err;
165         struct iattr iattr;
166         struct io_cb_data *cbd = ll_init_cb();
167
168         pg.pg = page;
169         pg.count = to;
170         pg.off = (((obd_off)page->index) << PAGE_SHIFT);
171         pg.flag = create ? OBD_BRW_CREATE : 0;
172
173         ENTRY;
174         if (!cbd)
175                 RETURN(-ENOMEM);
176
177         SetPageUptodate(page);
178
179         if (!PageLocked(page))
180                 LBUG();
181
182         CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
183                from, to, (unsigned long long)pg.count);
184
185         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md,
186                       1, &pg, ll_sync_io_cb, cbd);
187         kunmap(page);
188
189         iattr.ia_size = pg.off + pg.count;
190         if (iattr.ia_size > inode->i_size) {
191                 /* do NOT truncate when writing in the middle of a file */
192                 inode->i_size = iattr.ia_size;
193                 iattr.ia_valid = ATTR_SIZE;
194 #if 0
195                 err = ll_inode_setattr(inode, &iattr, 0);
196                 if (err) {
197                         CERROR("failed - %d.\n", err);
198                         err = -EIO;
199                 }
200 #endif
201         }
202         RETURN(err);
203 } /* ll_commit_write */
204
205 void ll_truncate(struct inode *inode)
206 {
207         struct obdo oa = {0};
208         struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
209         struct lustre_handle *lockhs = NULL;
210         int err;
211         ENTRY;
212
213         if (!md) {
214                 /* object not yet allocated */
215                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
216                 return;
217         }
218
219         oa.o_id = md->lmd_object_id;
220         oa.o_size = inode->i_size;
221
222         CDEBUG(D_INFO, "calling punch for %ld (all bytes after %Ld)\n",
223                (long)oa.o_id, (unsigned long long)oa.o_size);
224
225         err = ll_size_lock(inode, md, oa.o_size, LCK_PW, &lockhs);
226         if (err) {
227                 CERROR("ll_size_lock failed: %d\n", err);
228                 /* FIXME: What to do here?  It's too late to back out... */
229                 LBUG();
230         }
231
232         oa.o_valid = OBD_MD_FLID;
233         /* truncate == punch to/from start from/to end:
234            set end to -1 for that. */
235         err = obd_punch(ll_i2obdconn(inode), &oa, md, inode->i_size,
236                         0xffffffffffffffff);
237         if (err)
238                 CERROR("obd_truncate fails (%d)\n", err);
239         else
240                 /* This is done for us at the OST and MDS, but the
241                  * updated timestamps are not sent back to us.
242                  * Needed for POSIX.
243                  */
244                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
245
246         err = ll_size_unlock(inode, md, LCK_PW, lockhs);
247         if (err)
248                 CERROR("ll_size_unlock failed: %d\n", err);
249
250         EXIT;
251         return;
252 } /* ll_truncate */
253
254 int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
255                  unsigned long blocknr, int blocksize)
256 {
257         obd_count        bufs_per_obdo = iobuf->nr_pages;
258         struct ll_inode_info *lli = ll_i2info(inode);
259         struct lov_stripe_md *md = lli->lli_smd;
260         struct brw_page *pga;
261         int              rc = 0;
262         int i;
263         struct io_cb_data *cbd = ll_init_cb();
264
265         ENTRY;
266         if (!cbd)
267                 RETURN(-ENOMEM);
268
269         if (blocksize != PAGE_SIZE) {
270                 CERROR("direct_IO blocksize != PAGE_SIZE\n");
271                 return -EINVAL;
272         }
273
274         OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
275         if (!pga)
276                 GOTO(out, rc = -ENOMEM);
277
278         /* NB: we can't use iobuf->maplist[i]->index for the offset
279          * instead of "blocknr" because ->index contains garbage.
280          */
281         for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
282                 pga[i].pg = iobuf->maplist[i];
283                 pga[i].count = PAGE_SIZE;
284                 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
285                 pga[i].flag = OBD_BRW_CREATE;
286         }
287
288         if (!md || !md->lmd_object_id)
289                 GOTO(out, rc = -ENOMEM);
290
291         rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
292                      ll_i2obdconn(inode), md, bufs_per_obdo, pga,
293                      ll_sync_io_cb, cbd);
294         if (rc == 0)
295                 rc = bufs_per_obdo * PAGE_SIZE;
296
297 out:
298         OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
299         RETURN(rc);
300 }
301
302
303 int ll_flush_inode_pages(struct inode * inode)
304 {
305         obd_count        bufs_per_obdo = 0;
306         obd_size         *count = NULL;
307         obd_off          *offset = NULL;
308         obd_flag         *flags = NULL;
309         int              err = 0;
310
311         ENTRY;
312
313         spin_lock(&pagecache_lock);
314
315         spin_unlock(&pagecache_lock);
316
317
318         OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
319         OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
320         OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
321         if (!count || !offset || !flags)
322                 GOTO(out, err=-ENOMEM);
323
324 #if 0
325         for (i = 0 ; i < bufs_per_obdo ; i++) {
326                 count[i] = PAGE_SIZE;
327                 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
328                 flags[i] = OBD_BRW_CREATE;
329         }
330
331         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
332                       ll_i2info(inode)->lli_smd, bufs_per_obdo,
333                       iobuf->maplist, count, offset, flags, NULL, NULL);
334         if (err == 0)
335                 err = bufs_per_obdo * 4096;
336 #endif
337  out:
338         OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
339         OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
340         OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
341         RETURN(err);
342 }
343
344
345
346 struct address_space_operations ll_aops = {
347         readpage: ll_readpage,
348         writepage: ll_writepage,
349 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
350         direct_IO: ll_direct_IO,
351 #endif
352         sync_page: block_sync_page,
353         prepare_write: ll_prepare_write,
354         commit_write: ll_commit_write,
355         bmap: NULL
356 };