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