Whamcloud - gitweb
c2b736ac77689e9f434cbf2cd8fd97ed719be637
[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         }
203         EXIT;
204         return;
205 } /* ll_truncate */
206
207 int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
208                  unsigned long blocknr, int blocksize)
209 {
210         int i;
211         obd_count        num_obdo = 1;
212         obd_count        bufs_per_obdo = iobuf->nr_pages;
213         struct obdo     *oa = NULL;
214         obd_size         *count = NULL;
215         obd_off          *offset = NULL;
216         obd_flag         *flags = NULL;
217         int              rc = 0;
218
219         ENTRY;
220
221         if (blocksize != PAGE_SIZE) {
222                 CERROR("direct_IO blocksize != PAGE_SIZE, what to do?\n");
223                 LBUG();
224         }
225
226         OBD_ALLOC(count, sizeof(obd_size) * bufs_per_obdo);
227         OBD_ALLOC(offset, sizeof(obd_off) * bufs_per_obdo);
228         OBD_ALLOC(flags, sizeof(obd_flag) * bufs_per_obdo);
229         if (!count || !offset || !flags)
230                 GOTO(out, rc = -ENOMEM);
231
232         /* NB: we can't use iobuf->maplist[i]->index for the offset
233          * instead of "blocknr" because ->index contains garbage.
234          */
235         for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
236                 count[i] = PAGE_SIZE;
237                 offset[i] = (obd_off)blocknr << PAGE_SHIFT;
238                 flags[i] = OBD_BRW_CREATE;
239         }
240
241         oa = ll_i2info(inode)->lli_obdo;
242         if (!oa)
243                 GOTO(out, rc = -ENOMEM);
244         rc = obd_brw(rw, ll_i2obdconn(inode), num_obdo, &oa, &bufs_per_obdo,
245                       iobuf->maplist, count, offset, flags, NULL);
246         if (rc == 0) 
247                 rc = bufs_per_obdo * PAGE_SIZE;
248
249  out:
250         if (flags) 
251                 OBD_FREE(flags, sizeof(obd_flag) * bufs_per_obdo); 
252         if (count) 
253                 OBD_FREE(count, sizeof(obd_count) * bufs_per_obdo); 
254         if (offset) 
255                 OBD_FREE(offset, sizeof(obd_off) * bufs_per_obdo); 
256         RETURN(rc);
257 }
258
259
260 int ll_flush_inode_pages(struct inode * inode)
261 {
262         //int i;
263         //        obd_count        num_obdo = 1;
264         obd_count        bufs_per_obdo = 0;
265         struct obdo     *oa = NULL;
266         obd_size         *count = NULL;
267         obd_off          *offset = NULL;
268         obd_flag         *flags = NULL;
269         int              err = 0;
270
271         ENTRY;
272
273         spin_lock(&pagecache_lock);
274
275         spin_unlock(&pagecache_lock);
276
277
278         OBD_ALLOC(count, sizeof(obd_size) * bufs_per_obdo); 
279         if (!count)
280                 GOTO(out, err=-ENOMEM); 
281
282         OBD_ALLOC(offset, sizeof(obd_off) * bufs_per_obdo); 
283         if (!offset)
284                 GOTO(out, err=-ENOMEM); 
285
286         OBD_ALLOC(flags, sizeof(obd_flag) * bufs_per_obdo); 
287         if (!flags)
288                 GOTO(out, err=-ENOMEM); 
289
290 #if 0
291         for (i = 0 ; i < bufs_per_obdo ; i++) { 
292                 count[i] = PAGE_SIZE;
293                 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
294                 flags[i] = OBD_BRW_CREATE;
295         }
296
297         oa = ll_oa_from_inode(inode, OBD_MD_FLNOTOBD);
298         if (!oa)
299                 RETURN(-ENOMEM);
300
301         err = obd_brw(rw, ll_i2obdconn(inode), num_obdo, &oa, &bufs_per_obdo,
302                       iobuf->maplist, count, offset, flags);
303         if (err == 0) 
304                 err = bufs_per_obdo * 4096;
305 #endif
306  out:
307         if (oa) 
308                 obdo_free(oa);
309         if (flags) 
310                 OBD_FREE(flags, sizeof(obd_flag) * bufs_per_obdo); 
311         if (count) 
312                 OBD_FREE(count, sizeof(obd_count) * bufs_per_obdo); 
313         if (offset) 
314                 OBD_FREE(offset, sizeof(obd_off) * bufs_per_obdo); 
315         RETURN(err);
316 }
317
318
319
320 struct address_space_operations ll_aops = {
321         readpage: ll_readpage,
322         writepage: ll_writepage,
323 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,17))
324         direct_IO: ll_direct_IO,
325 #endif
326         sync_page: block_sync_page,
327         prepare_write: ll_prepare_write, 
328         commit_write: ll_commit_write,
329         bmap: NULL
330 };