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