Whamcloud - gitweb
ee1631ed614bc5103df5053041cfe8db43505583
[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) 2001, 2002 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/config.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/iobuf.h>
30 #include <linux/errno.h>
31 #include <linux/smp_lock.h>
32 #include <linux/unistd.h>
33 #include <linux/version.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36
37 #include <linux/fs.h>
38 #include <linux/stat.h>
39 #include <asm/uaccess.h>
40 #include <asm/segment.h>
41 #include <linux/mm.h>
42 #include <linux/pagemap.h>
43 #include <linux/smp_lock.h>
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
47 #include <linux/lustre_mds.h>
48 #include <linux/lustre_lite.h>
49 #include <linux/lustre_lib.h>
50
51 /*
52  * Remove page from dirty list
53  */
54 static void __set_page_clean(struct page *page)
55 {
56         struct address_space *mapping = page->mapping;
57         struct inode *inode;
58
59         if (!mapping)
60                 return;
61
62 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
63         spin_lock(&pagecache_lock);
64 #endif
65
66         list_del(&page->list);
67         list_add(&page->list, &mapping->clean_pages);
68
69         inode = mapping->host;
70         if (list_empty(&mapping->dirty_pages)) {
71                 CDEBUG(D_INODE, "inode clean\n");
72                 inode->i_state &= ~I_DIRTY_PAGES;
73         }
74 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
75         spin_unlock(&pagecache_lock);
76 #endif
77         EXIT;
78 }
79
80 inline void set_page_clean(struct page *page)
81 {
82         if (PageDirty(page)) {
83                 ClearPageDirty(page);
84                 __set_page_clean(page);
85         }
86 }
87
88 /* SYNCHRONOUS I/O to object storage for an inode */
89 static int ll_brw(int cmd, struct inode *inode, struct page *page, int create)
90 {
91         struct ll_inode_info *lli = ll_i2info(inode);
92         struct lov_stripe_md *lsm = lli->lli_smd;
93         struct obd_brw_set *set;
94         struct brw_page pg;
95         int rc;
96         ENTRY;
97
98         set = obd_brw_set_new();
99         if (set == NULL)
100                 RETURN(-ENOMEM);
101
102         pg.pg = page;
103         pg.off = ((obd_off)page->index) << PAGE_SHIFT;
104
105         if (cmd == OBD_BRW_WRITE && (pg.off + PAGE_SIZE > inode->i_size))
106                 pg.count = inode->i_size % PAGE_SIZE;
107         else
108                 pg.count = PAGE_SIZE;
109
110         pg.flag = create ? OBD_BRW_CREATE : 0;
111
112         set->brw_callback = ll_brw_sync_wait;
113         rc = obd_brw(cmd, ll_i2obdconn(inode), lsm, 1, &pg, set);
114         if (rc) {
115                 if (rc != -EIO)
116                         CERROR("error from obd_brw: rc = %d\n", rc);
117         } else {
118                 rc = ll_brw_sync_wait(set, CB_PHASE_START);
119                 if (rc)
120                         CERROR("error from callback: rc = %d\n", rc);
121         }
122         obd_brw_set_free(set);
123
124         RETURN(rc);
125 }
126
127 /* returns the page unlocked, but with a reference */
128 static int ll_readpage(struct file *file, struct page *page)
129 {
130         struct inode *inode = page->mapping->host;
131         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
132         int rc = 0;
133         ENTRY;
134
135         if (!PageLocked(page))
136                 LBUG();
137
138         if (inode->i_size <= offset) {
139                 memset(kmap(page), 0, PAGE_SIZE);
140                 kunmap(page);
141                 GOTO(readpage_out, rc);
142         }
143
144         if (PageUptodate(page)) {
145                 CERROR("Explain this please?\n");
146                 GOTO(readpage_out, rc);
147         }
148
149         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
150         EXIT;
151
152  readpage_out:
153         if (!rc)
154                 SetPageUptodate(page);
155         unlock_page(page);
156         return 0;
157 } /* ll_readpage */
158
159 void ll_truncate(struct inode *inode)
160 {
161         struct obdo oa = {0};
162         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
163         struct lustre_handle *lockhs = NULL;
164         int err;
165         ENTRY;
166
167         if (!lsm) {
168                 /* object not yet allocated */
169                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
170                 return;
171         }
172
173         oa.o_id = lsm->lsm_object_id;
174         oa.o_mode = inode->i_mode;
175         oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
176
177         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after "LPD64")\n",
178                oa.o_id, inode->i_size);
179
180         err = ll_size_lock(inode, lsm, inode->i_size, LCK_PW, &lockhs);
181         if (err) {
182                 CERROR("ll_size_lock failed: %d\n", err);
183                 /* FIXME: What to do here?  It's too late to back out... */
184                 LBUG();
185         }
186
187         /* truncate == punch from new size to absolute end of file */
188         err = obd_punch(ll_i2obdconn(inode), &oa, lsm, inode->i_size,
189                         OBD_OBJECT_EOF);
190         if (err) { 
191                 LBUG();
192                 CERROR("obd_truncate fails (%d) ino %lu\n", err,
193                        inode->i_ino);
194         } else
195                 obdo_to_inode(inode, &oa, oa.o_valid);
196
197         err = ll_size_unlock(inode, lsm, LCK_PW, lockhs);
198         if (err)
199                 CERROR("ll_size_unlock failed: %d\n", err);
200
201         EXIT;
202         return;
203 } /* ll_truncate */
204
205 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
206
207 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
208                             unsigned to)
209 {
210         struct inode *inode = page->mapping->host;
211         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
212         int rc = 0;
213         char *addr;
214         ENTRY;
215
216         addr = kmap(page);
217         if (!PageLocked(page))
218                 LBUG();
219
220         if (Page_Uptodate(page))
221                 GOTO(prepare_done, rc);
222
223         /* We're completely overwriting an existing page, so _don't_ set it up
224          * to date until commit_write */
225         if (from == 0 && to == PAGE_SIZE)
226                 RETURN(0);
227
228         /* We are writing to a new page, no need to read old data */
229         if (inode->i_size <= offset) {
230                 memset(addr, 0, PAGE_SIZE);
231                 GOTO(prepare_done, rc=0);
232         }
233
234         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
235
236         EXIT;
237  prepare_done:
238         if (!rc)
239                 SetPageUptodate(page);
240
241         return rc;
242 }
243
244 /* returns the page unlocked, but with a reference */
245 static int ll_writepage(struct page *page)
246 {
247         struct inode *inode = page->mapping->host;
248         int err;
249         ENTRY;
250
251         if (!PageLocked(page))
252                 LBUG();
253
254         err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
255         if ( !err ) {
256                 //SetPageUptodate(page);
257                 set_page_clean(page);
258         } else {
259                 CERROR("ll_brw failure %d\n", err);
260         }
261         unlock_page(page);
262         RETURN(err);
263 }
264
265
266 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
267  * too */
268 static int ll_commit_write(struct file *file, struct page *page,
269                            unsigned from, unsigned to)
270 {
271         struct inode *inode = page->mapping->host;
272         struct ll_inode_info *lli = ll_i2info(inode);
273         struct lov_stripe_md *md = lli->lli_smd;
274         struct brw_page pg;
275         struct obd_brw_set *set;
276         int rc, create = 1;
277         loff_t size;
278         ENTRY;
279
280         pg.pg = page;
281         pg.count = to;
282         pg.off = (((obd_off)page->index) << PAGE_SHIFT);
283         pg.flag = create ? OBD_BRW_CREATE : 0;
284
285         set = obd_brw_set_new();
286         if (set == NULL)
287                 RETURN(-ENOMEM);
288
289         SetPageUptodate(page);
290
291         if (!PageLocked(page))
292                 LBUG();
293
294         CDEBUG(D_INODE, "commit_page writing (off "LPD64"), count "LPD64"\n",
295                pg.off, pg.count);
296
297         set->brw_callback = ll_brw_sync_wait;
298         rc = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md, 1, &pg, set);
299         if (rc)
300                 CERROR("error from obd_brw: rc = %d\n", rc);
301         else {
302                 rc = ll_brw_sync_wait(set, CB_PHASE_START);
303                 if (rc)
304                         CERROR("error from callback: rc = %d\n", rc);
305         }
306         obd_brw_set_free(set);
307         kunmap(page);
308
309         size = pg.off + pg.count;
310         /* do NOT truncate when writing in the middle of a file */
311         if (size > inode->i_size)
312                 inode->i_size = size;
313
314         RETURN(rc);
315 } /* ll_commit_write */
316
317
318 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
319                         unsigned long blocknr, int blocksize)
320 {
321         obd_count bufs_per_obdo = iobuf->nr_pages;
322         struct ll_inode_info *lli = ll_i2info(inode);
323         struct lov_stripe_md *lsm = lli->lli_smd;
324         struct brw_page *pga;
325         struct obd_brw_set *set;
326         int i, rc = 0;
327         ENTRY;
328
329         if (!lsm || !lsm->lsm_object_id)
330                 RETURN(-ENOMEM);
331
332         if (blocksize != PAGE_SIZE) {
333                 CERROR("direct_IO blocksize != PAGE_SIZE\n");
334                 RETURN(-EINVAL);
335         }
336
337         set = obd_brw_set_new();
338         if (set == NULL)
339                 RETURN(-ENOMEM);
340
341         OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
342         if (!pga) {
343                 obd_brw_set_free(set);
344                 RETURN(-ENOMEM);
345         }
346
347         /* NB: we can't use iobuf->maplist[i]->index for the offset
348          * instead of "blocknr" because ->index contains garbage.
349          */
350         for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
351                 pga[i].pg = iobuf->maplist[i];
352                 pga[i].count = PAGE_SIZE;
353                 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
354                 pga[i].flag = OBD_BRW_CREATE;
355         }
356
357         set->brw_callback = ll_brw_sync_wait;
358         rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
359                      ll_i2obdconn(inode), lsm, bufs_per_obdo, pga, set);
360         if (rc)
361                 CERROR("error from obd_brw: rc = %d\n", rc);
362         else {
363                 rc = ll_brw_sync_wait(set, CB_PHASE_START);
364                 if (rc)
365                         CERROR("error from callback: rc = %d\n", rc);
366         }
367         obd_brw_set_free(set);
368         if (rc == 0)
369                 rc = bufs_per_obdo * PAGE_SIZE;
370
371         OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
372         RETURN(rc);
373 }
374
375 int ll_flush_inode_pages(struct inode * inode)
376 {
377         obd_count        bufs_per_obdo = 0;
378         obd_size         *count = NULL;
379         obd_off          *offset = NULL;
380         obd_flag         *flags = NULL;
381         int              err = 0;
382
383         ENTRY;
384
385         spin_lock(&pagecache_lock);
386
387         spin_unlock(&pagecache_lock);
388
389
390         OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
391         OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
392         OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
393         if (!count || !offset || !flags)
394                 GOTO(out, err=-ENOMEM);
395
396 #if 0
397         for (i = 0 ; i < bufs_per_obdo ; i++) {
398                 count[i] = PAGE_SIZE;
399                 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
400                 flags[i] = OBD_BRW_CREATE;
401         }
402
403         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
404                       ll_i2info(inode)->lli_smd, bufs_per_obdo,
405                       iobuf->maplist, count, offset, flags, NULL, NULL);
406         if (err == 0)
407                 err = bufs_per_obdo * 4096;
408 #endif
409  out:
410         OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
411         OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
412         OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
413         RETURN(err);
414 }
415
416 #endif
417
418
419 struct address_space_operations ll_aops = {
420         readpage: ll_readpage,
421 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
422         direct_IO: ll_direct_IO,
423         writepage: ll_writepage,
424         sync_page: block_sync_page,
425         prepare_write: ll_prepare_write,
426         commit_write: ll_commit_write,
427         bmap: NULL
428 #endif
429 };