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