Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[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         CDEBUG(D_PAGE, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
111               cmd & OBD_BRW_WRITE ? "write" : "read", pg.count, inode->i_ino,
112               pg.off, pg.off);
113         if (pg.count == 0) {
114                 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
115                        LPU64"\n",
116                        inode->i_ino, inode, inode->i_size, page->mapping->host,
117                        page->mapping->host->i_size, page->index, pg.off);
118         }
119
120         pg.flag = create ? OBD_BRW_CREATE : 0;
121
122         set->brw_callback = ll_brw_sync_wait;
123         rc = obd_brw(cmd, ll_i2obdconn(inode), lsm, 1, &pg, set);
124         if (rc) {
125                 if (rc != -EIO)
126                         CERROR("error from obd_brw: rc = %d\n", rc);
127         } else {
128                 rc = ll_brw_sync_wait(set, CB_PHASE_START);
129                 if (rc)
130                         CERROR("error from callback: rc = %d\n", rc);
131         }
132         obd_brw_set_free(set);
133
134         RETURN(rc);
135 }
136
137 /* returns the page unlocked, but with a reference */
138 static int ll_readpage(struct file *file, struct page *page)
139 {
140         struct inode *inode = page->mapping->host;
141         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
142         int rc = 0;
143         ENTRY;
144
145         if (!PageLocked(page))
146                 LBUG();
147
148         if (inode->i_size <= offset) {
149                 memset(kmap(page), 0, PAGE_SIZE);
150                 kunmap(page);
151                 GOTO(readpage_out, rc);
152         }
153
154         if (PageUptodate(page)) {
155                 CERROR("Explain this please?\n");
156                 GOTO(readpage_out, rc);
157         }
158
159         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
160         EXIT;
161
162  readpage_out:
163         if (!rc)
164                 SetPageUptodate(page);
165         unlock_page(page);
166         return 0;
167 } /* ll_readpage */
168
169 void ll_truncate(struct inode *inode)
170 {
171         struct obdo oa = {0};
172         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
173         struct lustre_handle lockh = { 0, 0 };
174         int err;
175         ENTRY;
176
177         if (!lsm) {
178                 /* object not yet allocated */
179                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
180                 return;
181         }
182
183         oa.o_id = lsm->lsm_object_id;
184         oa.o_mode = inode->i_mode;
185         oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
186
187         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
188                oa.o_id, inode->i_size);
189
190         err = ll_size_lock(inode, lsm, inode->i_size, LCK_PW, &lockh);
191         if (err) {
192                 CERROR("ll_size_lock failed: %d\n", err);
193                 return;
194         }
195
196         /* truncate == punch from new size to absolute end of file */
197         err = obd_punch(ll_i2obdconn(inode), &oa, lsm, inode->i_size,
198                         OBD_OBJECT_EOF);
199         if (err)
200                 CERROR("obd_truncate fails (%d) ino %lu\n", err, inode->i_ino);
201         else
202                 obdo_to_inode(inode, &oa, oa.o_valid);
203
204         err = ll_size_unlock(inode, lsm, LCK_PW, &lockh);
205         if (err)
206                 CERROR("ll_size_unlock failed: %d\n", err);
207
208         EXIT;
209         return;
210 } /* ll_truncate */
211
212 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
213
214 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
215                             unsigned to)
216 {
217         struct inode *inode = page->mapping->host;
218         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
219         int rc = 0;
220         char *addr;
221         ENTRY;
222
223         addr = kmap(page);
224         if (!PageLocked(page))
225                 LBUG();
226
227         if (Page_Uptodate(page))
228                 GOTO(prepare_done, rc);
229
230         /* We're completely overwriting an existing page, so _don't_ set it up
231          * to date until commit_write */
232         if (from == 0 && to == PAGE_SIZE)
233                 RETURN(0);
234
235         /* We are writing to a new page, no need to read old data */
236         if (inode->i_size <= offset) {
237                 memset(addr, 0, PAGE_SIZE);
238                 GOTO(prepare_done, rc=0);
239         }
240
241         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
242
243         EXIT;
244  prepare_done:
245         if (!rc)
246                 SetPageUptodate(page);
247
248         return rc;
249 }
250
251 /* Write a page from kupdated or kswapd.
252  *
253  * We unlock the page even in the face of an error, otherwise dirty
254  * pages could OOM the system if they cannot be written.  Also, there
255  * is nobody to return an error code to from here - the application
256  * may not even be running anymore.
257  *
258  * Returns the page unlocked, but with a reference.
259  */
260 static int ll_writepage(struct page *page) {
261         struct inode *inode = page->mapping->host; int err; ENTRY;
262
263         LASSERT(PageLocked(page));
264
265         /* XXX need to make sure we have LDLM lock on this page */
266         err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
267         if (err)
268                 CERROR("ll_brw failure %d\n", err);
269         else
270                 set_page_clean(page);
271
272         unlock_page(page);
273         RETURN(err);
274 }
275
276
277 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
278  * too */
279 static int ll_commit_write(struct file *file, struct page *page,
280                            unsigned from, unsigned to)
281 {
282         struct inode *inode = page->mapping->host;
283         struct ll_inode_info *lli = ll_i2info(inode);
284         struct lov_stripe_md *md = lli->lli_smd;
285         struct brw_page pg;
286         struct obd_brw_set *set;
287         int rc, create = 1;
288         loff_t size;
289         ENTRY;
290
291         pg.pg = page;
292         pg.count = to;
293         /* XXX make the starting offset "from" */
294         pg.off = (((obd_off)page->index) << PAGE_SHIFT);
295         pg.flag = create ? OBD_BRW_CREATE : 0;
296
297         set = obd_brw_set_new();
298         if (set == NULL)
299                 RETURN(-ENOMEM);
300
301         SetPageUptodate(page);
302
303         if (!PageLocked(page))
304                 LBUG();
305
306         CDEBUG(D_INODE, "commit_page writing (off "LPD64"), count %d\n",
307                pg.off, pg.count);
308
309         set->brw_callback = ll_brw_sync_wait;
310         rc = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md, 1, &pg, set);
311         if (rc)
312                 CERROR("error from obd_brw: rc = %d\n", rc);
313         else {
314                 rc = ll_brw_sync_wait(set, CB_PHASE_START);
315                 if (rc)
316                         CERROR("error from callback: rc = %d\n", rc);
317         }
318         obd_brw_set_free(set);
319         kunmap(page);
320
321         size = pg.off + pg.count;
322         /* do NOT truncate when writing in the middle of a file */
323         if (size > inode->i_size)
324                 inode->i_size = size;
325
326         RETURN(rc);
327 } /* ll_commit_write */
328
329
330 static int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
331                         unsigned long blocknr, int blocksize)
332 {
333         obd_count bufs_per_obdo = iobuf->nr_pages;
334         struct ll_inode_info *lli = ll_i2info(inode);
335         struct lov_stripe_md *lsm = lli->lli_smd;
336         struct brw_page *pga;
337         struct obd_brw_set *set;
338         int i, rc = 0;
339         ENTRY;
340
341         if (!lsm || !lsm->lsm_object_id)
342                 RETURN(-ENOMEM);
343
344         if (blocksize != PAGE_SIZE) {
345                 CERROR("direct_IO blocksize != PAGE_SIZE\n");
346                 RETURN(-EINVAL);
347         }
348
349         set = obd_brw_set_new();
350         if (set == NULL)
351                 RETURN(-ENOMEM);
352
353         OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
354         if (!pga) {
355                 obd_brw_set_free(set);
356                 RETURN(-ENOMEM);
357         }
358
359         /* NB: we can't use iobuf->maplist[i]->index for the offset
360          * instead of "blocknr" because ->index contains garbage.
361          */
362         for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
363                 pga[i].pg = iobuf->maplist[i];
364                 pga[i].count = PAGE_SIZE;
365                 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
366                 pga[i].flag = OBD_BRW_CREATE;
367         }
368
369         set->brw_callback = ll_brw_sync_wait;
370         rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
371                      ll_i2obdconn(inode), lsm, bufs_per_obdo, pga, set);
372         if (rc)
373                 CERROR("error from obd_brw: rc = %d\n", rc);
374         else {
375                 rc = ll_brw_sync_wait(set, CB_PHASE_START);
376                 if (rc)
377                         CERROR("error from callback: rc = %d\n", rc);
378         }
379         obd_brw_set_free(set);
380         if (rc == 0)
381                 rc = bufs_per_obdo * PAGE_SIZE;
382
383         OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
384         RETURN(rc);
385 }
386
387 int ll_flush_inode_pages(struct inode * inode)
388 {
389         obd_count        bufs_per_obdo = 0;
390         obd_size         *count = NULL;
391         obd_off          *offset = NULL;
392         obd_flag         *flags = NULL;
393         int              err = 0;
394
395         ENTRY;
396
397         spin_lock(&pagecache_lock);
398
399         spin_unlock(&pagecache_lock);
400
401
402         OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
403         OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
404         OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
405         if (!count || !offset || !flags)
406                 GOTO(out, err=-ENOMEM);
407
408 #if 0
409         for (i = 0 ; i < bufs_per_obdo ; i++) {
410                 count[i] = PAGE_SIZE;
411                 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
412                 flags[i] = OBD_BRW_CREATE;
413         }
414
415         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
416                       ll_i2info(inode)->lli_smd, bufs_per_obdo,
417                       iobuf->maplist, count, offset, flags, NULL, NULL);
418         if (err == 0)
419                 err = bufs_per_obdo * 4096;
420 #endif
421  out:
422         OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
423         OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
424         OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
425         RETURN(err);
426 }
427
428 #endif
429
430
431 struct address_space_operations ll_aops = {
432         readpage: ll_readpage,
433 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
434         direct_IO: ll_direct_IO,
435         writepage: ll_writepage,
436         sync_page: block_sync_page,
437         prepare_write: ll_prepare_write,
438         commit_write: ll_commit_write,
439         bmap: NULL
440 #endif
441 };