Whamcloud - gitweb
- change I/O to use a pagearray
[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 *lii = ll_i2info(inode);
41         struct lov_stripe_md *md = lii->lli_smd;
42         struct brw_page pg; 
43         int              err;
44         struct io_cb_data *cbd = ll_init_cb();
45         ENTRY;
46         if (!cbd) 
47                 RETURN(-ENOMEM); 
48
49         pg.pg = page;
50         pg.count = PAGE_SIZE;
51         pg.off = ((obd_off)page->index) << PAGE_SHIFT;
52         pg.flag = create ? OBD_BRW_CREATE : 0;
53
54         err = obd_brw(rw, ll_i2obdconn(inode), md, 1, &pg, ll_sync_io_cb, cbd);
55         RETURN(err);
56 } /* ll_brw */
57
58 /* returns the page unlocked, but with a reference */
59 static int ll_readpage(struct file *file, struct page *page)
60 {
61         struct inode *inode = page->mapping->host;
62         int rc = 0;
63         ENTRY;
64
65         if (!PageLocked(page))
66                 LBUG();
67
68         if (((inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT) <= page->index) {
69                 memset(kmap(page), 0, PAGE_SIZE);
70                 kunmap(page);
71                 GOTO(readpage_out, rc);
72         }
73
74         if (Page_Uptodate(page)) {
75                 CERROR("Explain this please?\n");
76                 GOTO(readpage_out, rc);
77         }
78
79         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
80         EXIT;
81
82  readpage_out:
83         if (!rc)
84                 SetPageUptodate(page);
85         UnlockPage(page);
86         return 0;
87 } /* ll_readpage */
88
89
90 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
91                             unsigned to)
92 {
93         struct inode *inode = page->mapping->host;
94         //obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
95         int rc = 0;
96         char *addr;
97         ENTRY; 
98         
99         addr = kmap(page);
100         if (!PageLocked(page))
101                 LBUG();
102
103         if (Page_Uptodate(page))
104                 GOTO(prepare_done, rc);
105
106         memset(addr, 0, PAGE_SIZE);
107
108         /* We're completely overwriting an existing page, so _don't_ set it up
109          * to date until commit_write */
110         if (from == 0 && to == PAGE_SIZE)
111                 RETURN(0);
112         
113         /* prepare write should not read what lies beyond the end of
114            the file */
115
116
117         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
118
119         EXIT;
120  prepare_done:
121         if (!rc)
122                 SetPageUptodate(page);
123
124         return rc;
125 }
126
127 /* returns the page unlocked, but with a reference */
128 static int ll_writepage(struct page *page)
129 {
130         struct inode *inode = page->mapping->host;
131         int err;
132         ENTRY;
133
134         if (!PageLocked(page))
135                 LBUG();
136
137         err = ll_brw(OBD_BRW_WRITE, inode, page, 1);
138         if ( !err ) {
139                 //SetPageUptodate(page);
140                 set_page_clean(page);
141         } else {
142                 CERROR("ll_brw failure %d\n", err);
143         }
144         UnlockPage(page); 
145         RETURN(err);
146 }
147
148
149 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
150  * too */
151 static int ll_commit_write(struct file *file, struct page *page,
152                            unsigned from, unsigned to)
153 {
154         int create = 1;
155         struct inode *inode = page->mapping->host;
156         struct ll_inode_info *lii = ll_i2info(inode);
157         struct lov_stripe_md *md = lii->lli_smd;
158         struct brw_page pg; 
159         int              err;
160         struct iattr     iattr;
161         struct io_cb_data *cbd = ll_init_cb();
162
163         pg.pg = page;
164         pg.count = to;
165         pg.off = (((obd_off)page->index) << PAGE_SHIFT);
166         pg.flag = create ? OBD_BRW_CREATE : 0;
167
168         ENTRY;
169         if (!cbd) 
170                 RETURN(-ENOMEM); 
171
172         SetPageUptodate(page);
173
174         if (!PageLocked(page))
175                 LBUG();
176
177         CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
178                from, to, (unsigned long long)pg.count);
179
180         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md,
181                       1, &pg, ll_sync_io_cb, cbd);
182         kunmap(page);
183
184         iattr.ia_size = pg.off + pg.count;
185         if (iattr.ia_size > inode->i_size) {
186                 /* do NOT truncate when writing in the middle of a file */
187                 inode->i_size = iattr.ia_size;
188                 iattr.ia_valid = ATTR_SIZE;
189 #if 0
190                 err = ll_inode_setattr(inode, &iattr, 0);
191                 if (err) {
192                         CERROR("failed - %d.\n", err);
193                         err = -EIO;
194                 }
195 #endif
196         }
197
198         RETURN(err);
199 } /* ll_commit_write */
200
201 void ll_truncate(struct inode *inode)
202 {
203         struct obdo oa = {0};
204         struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
205         int err;
206         ENTRY;
207
208         if (!md) { 
209                 /* object not yet allocated */
210                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
211                 return;
212         }
213
214         CDEBUG(D_INFO, "calling punch for %ld (all bytes after %Ld)\n",
215                (long)oa.o_id, (unsigned long long)oa.o_size);
216
217         oa.o_id = md->lmd_object_id;
218         oa.o_valid = OBD_MD_FLID;
219         /* truncate == punch to/from start from/to end:
220            set end to -1 for that. */
221         err = obd_punch(ll_i2obdconn(inode), &oa, md, inode->i_size,
222                         0xffffffffffffffff);
223         if (err)
224                 CERROR("obd_truncate fails (%d)\n", err);
225         else
226                 /* This is done for us at the OST and MDS, but the
227                  * updated timestamps are not sent back to us.
228                  * Needed for POSIX.
229                  */
230                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
231
232         EXIT;
233         return;
234 } /* ll_truncate */
235
236 int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
237                  unsigned long blocknr, int blocksize)
238 {
239         obd_count        bufs_per_obdo = iobuf->nr_pages;
240         struct ll_inode_info *lii = ll_i2info(inode);
241         struct lov_stripe_md *md = lii->lli_smd;
242         struct brw_page *pga; 
243         int              rc = 0;
244         int i;
245         struct io_cb_data *cbd = ll_init_cb();
246
247         ENTRY;
248         if (!cbd) 
249                 RETURN(-ENOMEM); 
250
251         if (blocksize != PAGE_SIZE) {
252                 CERROR("direct_IO blocksize != PAGE_SIZE\n");
253                 return -EINVAL;
254         }
255
256         OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
257         if (pga) 
258                 GOTO(out, rc = -ENOMEM);
259
260         /* NB: we can't use iobuf->maplist[i]->index for the offset
261          * instead of "blocknr" because ->index contains garbage.
262          */
263         for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
264                 pga[i].pg = iobuf->maplist[i];
265                 pga[i].count = PAGE_SIZE;
266                 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
267                 pga[i].flag = OBD_BRW_CREATE;
268         }
269
270         if (!md || !md->lmd_object_id)
271                 GOTO(out, rc = -ENOMEM);
272
273         rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
274                      ll_i2obdconn(inode), md, bufs_per_obdo, pga,
275                      ll_sync_io_cb, cbd);
276         if (rc == 0)
277                 rc = bufs_per_obdo * PAGE_SIZE;
278
279 out:
280         OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
281         RETURN(rc);
282 }
283
284
285 int ll_flush_inode_pages(struct inode * inode)
286 {
287         obd_count        bufs_per_obdo = 0;
288         obd_size         *count = NULL;
289         obd_off          *offset = NULL;
290         obd_flag         *flags = NULL;
291         int              err = 0;
292
293         ENTRY;
294
295         spin_lock(&pagecache_lock);
296
297         spin_unlock(&pagecache_lock);
298
299
300         OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
301         OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
302         OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
303         if (!count || !offset || !flags)
304                 GOTO(out, err=-ENOMEM);
305
306 #if 0
307         for (i = 0 ; i < bufs_per_obdo ; i++) {
308                 count[i] = PAGE_SIZE;
309                 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
310                 flags[i] = OBD_BRW_CREATE;
311         }
312
313         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
314                       ll_i2info(inode)->lli_smd, bufs_per_obdo,
315                       iobuf->maplist, count, offset, flags, NULL, NULL);
316         if (err == 0)
317                 err = bufs_per_obdo * 4096;
318 #endif
319  out:
320         OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
321         OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
322         OBD_FREE(offset, sizeof(*offset) * bufs_per_obdo);
323         RETURN(err);
324 }
325
326
327
328 struct address_space_operations ll_aops = {
329         readpage: ll_readpage,
330         writepage: ll_writepage,
331 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
332         direct_IO: ll_direct_IO,
333 #endif
334         sync_page: block_sync_page,
335         prepare_write: ll_prepare_write,
336         commit_write: ll_commit_write,
337         bmap: NULL
338 };