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