Whamcloud - gitweb
Fix another nasty bug in ll_common_unlink - we were always setting the
[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 *lli = ll_i2info(inode);
41         struct lov_stripe_md *md = lli->lli_smd;
42         struct brw_page pg;
43         int err;
44         struct io_cb_data *cbd = ll_init_cb();
45         ENTRY;
46
47         if (!cbd)
48                 RETURN(-ENOMEM);
49
50         pg.pg = page;
51         pg.count = PAGE_SIZE;
52         pg.off = ((obd_off)page->index) << PAGE_SHIFT;
53         pg.flag = create ? OBD_BRW_CREATE : 0;
54
55         err = obd_brw(rw, ll_i2obdconn(inode), md, 1, &pg, ll_sync_io_cb, cbd);
56
57         RETURN(err);
58 } /* ll_brw */
59
60 /* returns the page unlocked, but with a reference */
61 static int ll_readpage(struct file *file, struct page *page)
62 {
63         struct inode *inode = page->mapping->host;
64         int rc = 0;
65         ENTRY;
66
67         if (!PageLocked(page))
68                 LBUG();
69
70         if (((inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT) <= page->index) {
71                 memset(kmap(page), 0, PAGE_SIZE);
72                 kunmap(page);
73                 GOTO(readpage_out, rc);
74         }
75
76         if (Page_Uptodate(page)) {
77                 CERROR("Explain this please?\n");
78                 GOTO(readpage_out, rc);
79         }
80
81         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
82         EXIT;
83
84  readpage_out:
85         if (!rc)
86                 SetPageUptodate(page);
87         UnlockPage(page);
88         return 0;
89 } /* ll_readpage */
90
91
92 static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
93                             unsigned to)
94 {
95         struct inode *inode = page->mapping->host;
96         //obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
97         int rc = 0;
98         char *addr;
99         ENTRY;
100
101         addr = kmap(page);
102         if (!PageLocked(page))
103                 LBUG();
104
105         if (Page_Uptodate(page))
106                 GOTO(prepare_done, rc);
107
108         memset(addr, 0, PAGE_SIZE);
109
110         /* We're completely overwriting an existing page, so _don't_ set it up
111          * to date until commit_write */
112         if (from == 0 && to == PAGE_SIZE)
113                 RETURN(0);
114
115         /* prepare write should not read what lies beyond the end of
116            the file */
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 *lli = ll_i2info(inode);
157         struct lov_stripe_md *md = lli->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         RETURN(err);
198 } /* ll_commit_write */
199
200 void ll_truncate(struct inode *inode)
201 {
202         struct obdo oa = {0};
203         struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
204         int err;
205         ENTRY;
206
207         if (!md) {
208                 /* object not yet allocated */
209                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
210                 return;
211         }
212
213         CDEBUG(D_INFO, "calling punch for %ld (all bytes after %Ld)\n",
214                (long)oa.o_id, (unsigned long long)oa.o_size);
215
216         oa.o_id = md->lmd_object_id;
217         oa.o_valid = OBD_MD_FLID;
218         /* truncate == punch to/from start from/to end:
219            set end to -1 for that. */
220         err = obd_punch(ll_i2obdconn(inode), &oa, md, inode->i_size,
221                         0xffffffffffffffff);
222         if (err)
223                 CERROR("obd_truncate fails (%d)\n", err);
224         else
225                 /* This is done for us at the OST and MDS, but the
226                  * updated timestamps are not sent back to us.
227                  * Needed for POSIX.
228                  */
229                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
230
231         EXIT;
232         return;
233 } /* ll_truncate */
234
235 int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
236                  unsigned long blocknr, int blocksize)
237 {
238         obd_count        bufs_per_obdo = iobuf->nr_pages;
239         struct ll_inode_info *lli = ll_i2info(inode);
240         struct lov_stripe_md *md = lli->lli_smd;
241         struct brw_page *pga;
242         int              rc = 0;
243         int i;
244         struct io_cb_data *cbd = ll_init_cb();
245
246         ENTRY;
247         if (!cbd)
248                 RETURN(-ENOMEM);
249
250         if (blocksize != PAGE_SIZE) {
251                 CERROR("direct_IO blocksize != PAGE_SIZE\n");
252                 return -EINVAL;
253         }
254
255         OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
256         if (!pga)
257                 GOTO(out, rc = -ENOMEM);
258
259         /* NB: we can't use iobuf->maplist[i]->index for the offset
260          * instead of "blocknr" because ->index contains garbage.
261          */
262         for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
263                 pga[i].pg = iobuf->maplist[i];
264                 pga[i].count = PAGE_SIZE;
265                 pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
266                 pga[i].flag = OBD_BRW_CREATE;
267         }
268
269         if (!md || !md->lmd_object_id)
270                 GOTO(out, rc = -ENOMEM);
271
272         rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
273                      ll_i2obdconn(inode), md, bufs_per_obdo, pga,
274                      ll_sync_io_cb, cbd);
275         if (rc == 0)
276                 rc = bufs_per_obdo * PAGE_SIZE;
277
278 out:
279         OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
280         RETURN(rc);
281 }
282
283
284 int ll_flush_inode_pages(struct inode * inode)
285 {
286         obd_count        bufs_per_obdo = 0;
287         obd_size         *count = NULL;
288         obd_off          *offset = NULL;
289         obd_flag         *flags = NULL;
290         int              err = 0;
291
292         ENTRY;
293
294         spin_lock(&pagecache_lock);
295
296         spin_unlock(&pagecache_lock);
297
298
299         OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
300         OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
301         OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
302         if (!count || !offset || !flags)
303                 GOTO(out, err=-ENOMEM);
304
305 #if 0
306         for (i = 0 ; i < bufs_per_obdo ; i++) {
307                 count[i] = PAGE_SIZE;
308                 offset[i] = ((obd_off)(iobuf->maplist[i])->index) << PAGE_SHIFT;
309                 flags[i] = OBD_BRW_CREATE;
310         }
311
312         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode),
313                       ll_i2info(inode)->lli_smd, bufs_per_obdo,
314                       iobuf->maplist, count, offset, flags, NULL, NULL);
315         if (err == 0)
316                 err = bufs_per_obdo * 4096;
317 #endif
318  out:
319         OBD_FREE(flags, sizeof(*flags) * bufs_per_obdo);
320         OBD_FREE(count, sizeof(*count) * bufs_per_obdo);
321         OBD_FREE(offset, sizeof(*offset) * 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 };