Whamcloud - gitweb
Land b_smallfix onto HEAD (20040512_1806)
[fs/lustre-release.git] / lustre / llite / file.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Andreas Dilger <adilger@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #define DEBUG_SUBSYSTEM S_LLITE
26 #include <linux/lustre_dlm.h>
27 #include <linux/lustre_lite.h>
28 #include <linux/pagemap.h>
29 #include <linux/file.h>
30 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
31 #include <linux/lustre_compat25.h>
32 #endif
33 #include "llite_internal.h"
34
35 int ll_mdc_close(struct obd_export *mdc_exp, struct inode *inode,
36                         struct file *file)
37 {
38         struct ll_file_data *fd = file->private_data;
39         struct ptlrpc_request *req = NULL;
40         struct obd_client_handle *och = &fd->fd_mds_och;
41         struct obdo obdo;
42         int rc;
43         ENTRY;
44
45         /* clear group lock, if present */
46         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
47                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
48                 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
49                 rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP,
50                                       &fd->fd_cwlockh);
51         }
52
53         obdo.o_id = inode->i_ino;
54         obdo.o_valid = OBD_MD_FLID;
55         obdo_from_inode(&obdo, inode, OBD_MD_FLTYPE | OBD_MD_FLMODE |
56                                       OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
57                                       OBD_MD_FLATIME | OBD_MD_FLMTIME |
58                                       OBD_MD_FLCTIME);
59         if (0 /* ll_is_inode_dirty(inode) */) {
60                 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
61                 obdo.o_valid |= OBD_MD_FLFLAGS;
62         }
63         rc = mdc_close(mdc_exp, &obdo, och, &req);
64         if (rc == EAGAIN) {
65                 /* We are the last writer, so the MDS has instructed us to get
66                  * the file size and any write cookies, then close again. */
67                 //ll_queue_done_writing(inode);
68                 rc = 0;
69         } else if (rc) {
70                 CERROR("inode %lu mdc close failed: rc = %d\n",
71                        inode->i_ino, rc);
72         }
73         if (rc == 0) {
74                 rc = ll_objects_destroy(req, file->f_dentry->d_inode);
75                 if (rc)
76                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
77                                inode->i_ino, rc);
78         }
79
80         mdc_clear_open_replay_data(och);
81         ptlrpc_req_finished(req);
82         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
83         file->private_data = NULL;
84         OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof *fd);
85
86         RETURN(rc);
87 }
88
89 /* While this returns an error code, fput() the caller does not, so we need
90  * to make every effort to clean up all of our state here.  Also, applications
91  * rarely check close errors and even if an error is returned they will not
92  * re-try the close call.
93  */
94 int ll_file_release(struct inode *inode, struct file *file)
95 {
96         struct ll_file_data *fd;
97         struct ll_sb_info *sbi = ll_i2sbi(inode);
98         int rc;
99
100         ENTRY;
101         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
102                inode->i_generation, inode);
103
104         /* don't do anything for / */
105         if (inode->i_sb->s_root == file->f_dentry)
106                 RETURN(0);
107
108         lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
109         fd = (struct ll_file_data *)file->private_data;
110         LASSERT(fd != NULL);
111
112         rc = ll_mdc_close(sbi->ll_mdc_exp, inode, file);
113         RETURN(rc);
114 }
115
116 static int ll_intent_file_open(struct file *file, void *lmm,
117                                int lmmsize, struct lookup_intent *itp)
118 {
119         struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
120         struct lustre_handle lockh;
121         struct mdc_op_data data;
122         struct dentry *parent = file->f_dentry->d_parent;
123         const char *name = file->f_dentry->d_name.name;
124         const int len = file->f_dentry->d_name.len;
125         int rc;
126
127         if (!parent)
128                 RETURN(-ENOENT);
129
130         ll_prepare_mdc_op_data(&data, parent->d_inode, NULL, name, len, O_RDWR);
131
132         rc = mdc_enqueue(sbi->ll_mdc_exp, LDLM_IBITS, itp, LCK_PW, &data,
133                          &lockh, lmm, lmmsize, ldlm_completion_ast,
134                          ll_mdc_blocking_ast, parent->d_inode);
135         if (rc < 0)
136                 CERROR("lock enqueue: err: %d\n", rc);
137         RETURN(rc);
138 }
139
140 int ll_local_open(struct file *file, struct lookup_intent *it)
141 {
142         struct ptlrpc_request *req = it->d.lustre.it_data;
143         struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
144         struct ll_file_data *fd;
145         struct mds_body *body;
146         ENTRY;
147
148         body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
149         LASSERT (body != NULL);                 /* reply already checked out */
150         LASSERT_REPSWABBED (req, 1);            /* and swabbed down */
151
152         LASSERT(!file->private_data);
153
154         OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
155         /* We can't handle this well without reorganizing ll_file_open and
156          * ll_mdc_close, so don't even try right now. */
157         LASSERT(fd != NULL);
158
159         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
160         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
161         file->private_data = fd;
162         ll_readahead_init(file->f_dentry->d_inode, &fd->fd_ras);
163
164         lli->lli_io_epoch = body->io_epoch;
165
166         mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
167
168         RETURN(0);
169 }
170
171 /* Open a file, and (for the very first open) create objects on the OSTs at
172  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
173  * creation or open until ll_lov_setstripe() ioctl is called.  We grab
174  * lli_open_sem to ensure no other process will create objects, send the
175  * stripe MD to the MDS, or try to destroy the objects if that fails.
176  *
177  * If we already have the stripe MD locally then we don't request it in
178  * mdc_open(), by passing a lmm_size = 0.
179  *
180  * It is up to the application to ensure no other processes open this file
181  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
182  * used.  We might be able to avoid races of that sort by getting lli_open_sem
183  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
184  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
185  */
186 int ll_file_open(struct inode *inode, struct file *file)
187 {
188         struct ll_inode_info *lli = ll_i2info(inode);
189         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
190                                           .it_flags = file->f_flags };
191         struct lov_stripe_md *lsm;
192         struct ptlrpc_request *req;
193         int rc = 0;
194         ENTRY;
195
196         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
197                inode->i_generation, inode);
198
199         /* don't do anything for / */
200         if (inode->i_sb->s_root == file->f_dentry)
201                 RETURN(0);
202
203         it = file->f_it;
204
205         if (!it || !it->d.lustre.it_disposition) {
206                 it = &oit;
207                 rc = ll_intent_file_open(file, NULL, 0, it);
208                 if (rc)
209                         GOTO(out, rc);
210         }
211
212         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
213         rc = it_open_error(DISP_OPEN_OPEN, it);
214         if (rc)
215                 GOTO(out, rc);
216
217         rc = ll_local_open(file, it);
218         if (rc)
219                 LBUG();
220
221         if (!S_ISREG(inode->i_mode))
222                 GOTO(out, rc);
223
224         lsm = lli->lli_smd;
225         if (lsm == NULL) {
226                 if (file->f_flags & O_LOV_DELAY_CREATE ||
227                     !(file->f_mode & FMODE_WRITE)) {
228                         CDEBUG(D_INODE, "object creation was delayed\n");
229                         GOTO(out, rc);
230                 }
231         }
232         file->f_flags &= ~O_LOV_DELAY_CREATE;
233         GOTO(out, rc);
234  out:
235         req = it->d.lustre.it_data;
236         ptlrpc_req_finished(req);
237         if (rc == 0)
238                 ll_open_complete(inode);
239         return rc;
240 }
241
242 /* Fills the obdo with the attributes for the inode defined by lsm */
243 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
244                    struct obdo *oa)
245 {
246         struct ptlrpc_request_set *set;
247         int rc;
248         ENTRY;
249
250         LASSERT(lsm != NULL);
251
252         memset(oa, 0, sizeof *oa);
253         oa->o_id = lsm->lsm_object_id;
254         oa->o_mode = S_IFREG;
255         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
256                 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
257                 OBD_MD_FLCTIME;
258
259         set = ptlrpc_prep_set();
260         if (set == NULL) {
261                 CERROR ("ENOMEM allocing request set\n");
262                 rc = -ENOMEM;
263         } else {
264                 rc = obd_getattr_async(exp, oa, lsm, set);
265                 if (rc == 0)
266                         rc = ptlrpc_set_wait(set);
267                 ptlrpc_set_destroy(set);
268         }
269         if (rc)
270                 RETURN(rc);
271
272         oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
273                         OBD_MD_FLCTIME | OBD_MD_FLSIZE);
274         RETURN(0);
275 }
276
277 static inline void ll_remove_suid(struct inode *inode)
278 {
279         unsigned int mode;
280
281         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
282         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
283
284         /* was any of the uid bits set? */
285         mode &= inode->i_mode;
286         if (mode && !capable(CAP_FSETID)) {
287                 inode->i_mode &= ~mode;
288                 // XXX careful here - we cannot change the size
289         }
290 }
291
292 static int ll_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
293 {
294         struct ll_inode_info *lli = ll_i2info(inode);
295         struct lov_stripe_md *lsm = lli->lli_smd;
296         struct obd_export *exp = ll_i2obdexp(inode);
297         struct {
298                 char name[16];
299                 struct ldlm_lock *lock;
300                 struct lov_stripe_md *lsm;
301         } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
302         __u32 stripe, vallen = sizeof(stripe);
303         int rc;
304         ENTRY;
305
306         if (lsm->lsm_stripe_count == 1)
307                 RETURN(0);
308
309         /* get our offset in the lov */
310         rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
311         if (rc != 0) {
312                 CERROR("obd_get_info: rc = %d\n", rc);
313                 LBUG();
314         }
315         LASSERT(stripe < lsm->lsm_stripe_count);
316         RETURN(stripe);
317 }
318
319 /* Flush the page cache for an extent as its canceled.  When we're on an LOV,
320  * we get a lock cancellation for each stripe, so we have to map the obd's
321  * region back onto the stripes in the file that it held.
322  *
323  * No one can dirty the extent until we've finished our work and they can
324  * enqueue another lock.  The DLM protects us from ll_file_read/write here,
325  * but other kernel actors could have pages locked.
326  *
327  * Called with the DLM lock held. */
328 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
329                               struct ldlm_lock *lock, __u32 stripe)
330 {
331         ldlm_policy_data_t tmpex;
332         unsigned long start, end, count, skip, i, j;
333         struct page *page;
334         int rc, rc2, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
335         struct lustre_handle lockh;
336         ENTRY;
337
338         memcpy(&tmpex, &lock->l_policy_data, sizeof(tmpex));
339         CDEBUG(D_INODE|D_PAGE, "inode %lu(%p) ["LPU64"->"LPU64"] size: %llu\n",
340                inode->i_ino, inode, tmpex.l_extent.start, tmpex.l_extent.end,
341                inode->i_size);
342
343         /* our locks are page granular thanks to osc_enqueue, we invalidate the
344          * whole page. */
345         LASSERT((tmpex.l_extent.start & ~PAGE_CACHE_MASK) == 0);
346         LASSERT(((tmpex.l_extent.end + 1) & ~PAGE_CACHE_MASK) == 0);
347
348         count = ~0;
349         skip = 0;
350         start = tmpex.l_extent.start >> PAGE_CACHE_SHIFT;
351         end = tmpex.l_extent.end >> PAGE_CACHE_SHIFT;
352         if (lsm->lsm_stripe_count > 1) {
353                 count = lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
354                 skip = (lsm->lsm_stripe_count - 1) * count;
355                 start += start/count * skip + stripe * count;
356                 if (end != ~0)
357                         end += end/count * skip + stripe * count;
358         }
359         if (end < tmpex.l_extent.end >> PAGE_CACHE_SHIFT)
360                 end = ~0;
361
362         i = (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
363         if (i < end)
364                 end = i;
365
366         CDEBUG(D_INODE|D_PAGE, "walking page indices start: %lu j: %lu "
367                "count: %lu skip: %lu end: %lu%s\n", start, start % count,
368                count, skip, end, discard ? " (DISCARDING)" : "");
369
370         /* this is the simplistic implementation of page eviction at
371          * cancelation.  It is careful to get races with other page
372          * lockers handled correctly.  fixes from bug 20 will make it
373          * more efficient by associating locks with pages and with
374          * batching writeback under the lock explicitly. */
375         for (i = start, j = start % count; i <= end;
376              j++, i++, tmpex.l_extent.start += PAGE_CACHE_SIZE) {
377                 if (j == count) {
378                         CDEBUG(D_PAGE, "skip index %lu to %lu\n", i, i + skip);
379                         i += skip;
380                         j = 0;
381                         if (i > end)
382                                 break;
383                 }
384                 LASSERTF(tmpex.l_extent.start< lock->l_policy_data.l_extent.end,
385                          LPU64" >= "LPU64" start %lu i %lu end %lu\n",
386                          tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
387                          start, i, end);
388
389                 if (!mapping_has_pages(inode->i_mapping)) {
390                         CDEBUG(D_INODE|D_PAGE, "nothing left\n");
391                         break;
392                 }
393
394                 conditional_schedule();
395
396                 page = find_get_page(inode->i_mapping, i);
397                 if (page == NULL)
398                         continue;
399                 LL_CDEBUG_PAGE(D_PAGE, page, "lock page idx %lu ext "LPU64"\n",
400                                i, tmpex.l_extent.start);
401                 lock_page(page);
402
403                 /* page->mapping to check with racing against teardown */
404                 if (!discard && clear_page_dirty_for_io(page)) {
405                         rc = ll_call_writepage(inode, page);
406                         if (rc != 0)
407                                 CERROR("writepage of page %p failed: %d\n",
408                                        page, rc);
409                         /* either waiting for io to complete or reacquiring
410                          * the lock that the failed writepage released */
411                         lock_page(page);
412                 }
413
414                 tmpex.l_extent.end = tmpex.l_extent.start + PAGE_CACHE_SIZE - 1;
415                 /* check to see if another DLM lock covers this page */
416                 ldlm_lock2handle(lock, &lockh);
417                 rc2 = ldlm_lock_match(NULL, 
418                                       LDLM_FL_BLOCK_GRANTED|LDLM_FL_CBPENDING |
419                                       LDLM_FL_TEST_LOCK,
420                                       NULL, 0, &tmpex, 0, &lockh);
421                 if (rc2 == 0 && page->mapping != NULL) {
422                         // checking again to account for writeback's lock_page()
423                         LL_CDEBUG_PAGE(D_PAGE, page, "truncating\n");
424                         ll_truncate_complete_page(page);
425                 }
426                 unlock_page(page);
427                 page_cache_release(page);
428         }
429         LASSERTF(tmpex.l_extent.start <=
430                  (lock->l_policy_data.l_extent.end == ~0ULL ? ~0ULL :
431                   lock->l_policy_data.l_extent.end + 1),
432                  "loop too long "LPU64" > "LPU64" start %lu i %lu end %lu\n",
433                  tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
434                  start, i, end);
435         EXIT;
436 }
437
438 static int ll_extent_lock_callback(struct ldlm_lock *lock,
439                                    struct ldlm_lock_desc *new, void *data,
440                                    int flag)
441 {
442         struct lustre_handle lockh = { 0 };
443         int rc;
444         ENTRY;
445
446         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
447                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
448                 LBUG();
449         }
450
451         switch (flag) {
452         case LDLM_CB_BLOCKING:
453                 ldlm_lock2handle(lock, &lockh);
454                 rc = ldlm_cli_cancel(&lockh);
455                 if (rc != ELDLM_OK)
456                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
457                 break;
458         case LDLM_CB_CANCELING: {
459                 struct inode *inode;
460                 struct ll_inode_info *lli;
461                 struct lov_stripe_md *lsm;
462                 __u32 stripe;
463                 __u64 kms;
464
465                 /* This lock wasn't granted, don't try to evict pages */
466                 if (lock->l_req_mode != lock->l_granted_mode)
467                         RETURN(0);
468
469                 inode = ll_inode_from_lock(lock);
470                 if (inode == NULL)
471                         RETURN(0);
472                 lli = ll_i2info(inode);
473                 if (lli == NULL)
474                         goto iput;
475                 if (lli->lli_smd == NULL)
476                         goto iput;
477                 lsm = lli->lli_smd;
478
479                 stripe = ll_lock_to_stripe_offset(inode, lock);
480                 ll_pgcache_remove_extent(inode, lsm, lock, stripe);
481
482                 down(&inode->i_sem);
483                 kms = ldlm_extent_shift_kms(lock,
484                                             lsm->lsm_oinfo[stripe].loi_kms);
485                 
486                 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
487                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
488                                    lsm->lsm_oinfo[stripe].loi_kms, kms);
489                 lsm->lsm_oinfo[stripe].loi_kms = kms;
490                 up(&inode->i_sem);
491                 //ll_try_done_writing(inode);
492         iput:
493                 iput(inode);
494                 break;
495         }
496         default:
497                 LBUG();
498         }
499
500         RETURN(0);
501 }
502
503 #if 0
504 int ll_async_completion_ast(struct ldlm_lock *lock, int flags, void *data)
505 {
506         /* XXX ALLOCATE - 160 bytes */
507         struct inode *inode = ll_inode_from_lock(lock);
508         struct ll_inode_info *lli = ll_i2info(inode);
509         struct lustre_handle lockh = { 0 };
510         struct ost_lvb *lvb;
511         __u32 stripe;
512         ENTRY;
513
514         if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
515                      LDLM_FL_BLOCK_CONV)) {
516                 LBUG(); /* not expecting any blocked async locks yet */
517                 LDLM_DEBUG(lock, "client-side async enqueue returned a blocked "
518                            "lock, returning");
519                 ldlm_lock_dump(D_OTHER, lock, 0);
520                 ldlm_reprocess_all(lock->l_resource);
521                 RETURN(0);
522         }
523
524         LDLM_DEBUG(lock, "client-side async enqueue: granted/glimpsed");
525
526         stripe = ll_lock_to_stripe_offset(inode, lock);
527
528         if (lock->l_lvb_len) {
529                 struct lov_stripe_md *lsm = lli->lli_smd;
530                 __u64 kms;
531                 lvb = lock->l_lvb_data;
532                 lsm->lsm_oinfo[stripe].loi_rss = lvb->lvb_size;
533
534                 down(&inode->i_sem);
535                 kms = MAX(lsm->lsm_oinfo[stripe].loi_kms, lvb->lvb_size);
536                 kms = ldlm_extent_shift_kms(NULL, kms);
537                 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
538                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
539                                    lsm->lsm_oinfo[stripe].loi_kms, kms);
540                 lsm->lsm_oinfo[stripe].loi_kms = kms;
541                 up(&inode->i_sem);
542         }
543
544         iput(inode);
545         wake_up(&lock->l_waitq);
546
547         ldlm_lock2handle(lock, &lockh);
548         ldlm_lock_decref(&lockh, LCK_PR);
549         RETURN(0);
550 }
551 #endif
552
553 static int ll_glimpse_callback(struct ldlm_lock *lock, void *reqp)
554 {
555         struct ptlrpc_request *req = reqp;
556         struct inode *inode = ll_inode_from_lock(lock);
557         struct ll_inode_info *lli;
558         struct ost_lvb *lvb;
559         int rc, size = sizeof(*lvb), stripe = 0;
560         ENTRY;
561
562         if (inode == NULL)
563                 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
564         lli = ll_i2info(inode);
565         if (lli == NULL)
566                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
567         if (lli->lli_smd == NULL)
568                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
569
570         /* First, find out which stripe index this lock corresponds to. */
571         if (lli->lli_smd->lsm_stripe_count > 1)
572                 stripe = ll_lock_to_stripe_offset(inode, lock);
573
574         rc = lustre_pack_reply(req, 1, &size, NULL);
575         if (rc) {
576                 CERROR("lustre_pack_reply: %d\n", rc);
577                 GOTO(iput, rc);
578         }
579
580         lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
581         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
582
583         LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
584                    inode->i_size, stripe, lvb->lvb_size);
585         GOTO(iput, 0);
586  iput:
587         iput(inode);
588
589  out:
590         /* These errors are normal races, so we don't want to fill the console
591          * with messages by calling ptlrpc_error() */
592         if (rc == -ELDLM_NO_LOCK_DATA)
593                 lustre_pack_reply(req, 0, NULL, NULL);
594
595         req->rq_status = rc;
596         return rc;
597 }
598
599 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
600 __u64 lov_merge_blocks(struct lov_stripe_md *lsm);
601 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
602
603 /* NB: lov_merge_size will prefer locally cached writes if they extend the
604  * file (because it prefers KMS over RSS when larger) */
605 int ll_glimpse_size(struct inode *inode, struct ost_lvb *lvb)
606 {
607         struct ll_inode_info *lli = ll_i2info(inode);
608         struct ll_sb_info *sbi = ll_i2sbi(inode);
609         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
610         struct lustre_handle lockh = { 0 };
611         int rc, flags = LDLM_FL_HAS_INTENT;
612         ENTRY;
613
614         CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", inode->i_ino);
615
616         rc = obd_enqueue(sbi->ll_osc_exp, lli->lli_smd, LDLM_EXTENT, &policy,
617                          LCK_PR, &flags, ll_extent_lock_callback,
618                          ldlm_completion_ast, ll_glimpse_callback, inode,
619                          sizeof(*lvb), lustre_swab_ost_lvb, &lockh);
620         if (rc != 0) {
621                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
622                 RETURN(rc > 0 ? -EIO : rc);
623         }
624
625         lvb->lvb_size = lov_merge_size(lli->lli_smd, 0);
626         inode->i_blocks = lov_merge_blocks(lli->lli_smd);
627         //inode->i_mtime = lov_merge_mtime(lli->lli_smd, inode->i_mtime);
628
629         CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
630                lvb->lvb_size, lvb->lvb_blocks);
631
632         obd_cancel(sbi->ll_osc_exp, lli->lli_smd, LCK_PR, &lockh);
633
634         RETURN(rc);
635 }
636
637 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
638                    struct lov_stripe_md *lsm, int mode,
639                    ldlm_policy_data_t *policy, struct lustre_handle *lockh,
640                    int ast_flags)
641 {
642         struct ll_sb_info *sbi = ll_i2sbi(inode);
643         int rc;
644         ENTRY;
645
646         LASSERT(lockh->cookie == 0);
647
648         /* XXX phil: can we do this?  won't it screw the file size up? */
649         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
650             (sbi->ll_flags & LL_SBI_NOLCK))
651                 RETURN(0);
652
653         CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
654                inode->i_ino, policy->l_extent.start, policy->l_extent.end);
655
656         rc = obd_enqueue(sbi->ll_osc_exp, lsm, LDLM_EXTENT, policy, mode,
657                          &ast_flags, ll_extent_lock_callback,
658                          ldlm_completion_ast, ll_glimpse_callback, inode,
659                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
660         if (rc > 0)
661                 rc = -EIO;
662
663         if (policy->l_extent.start == 0 &&
664             policy->l_extent.end == OBD_OBJECT_EOF)
665                 inode->i_size = lov_merge_size(lsm, 1);
666
667         //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
668
669         RETURN(rc);
670 }
671
672 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
673                      struct lov_stripe_md *lsm, int mode,
674                      struct lustre_handle *lockh)
675 {
676         struct ll_sb_info *sbi = ll_i2sbi(inode);
677         int rc;
678         ENTRY;
679
680         /* XXX phil: can we do this?  won't it screw the file size up? */
681         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
682             (sbi->ll_flags & LL_SBI_NOLCK))
683                 RETURN(0);
684
685         rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
686
687         RETURN(rc);
688 }
689
690 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
691                             loff_t *ppos)
692 {
693         struct ll_file_data *fd = filp->private_data;
694         struct inode *inode = filp->f_dentry->d_inode;
695         struct ll_inode_info *lli = ll_i2info(inode);
696         struct lov_stripe_md *lsm = lli->lli_smd;
697         struct lustre_handle lockh = { 0 };
698         ldlm_policy_data_t policy;
699         int rc;
700         ssize_t retval;
701         __u64 kms;
702         ENTRY;
703         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
704                inode->i_ino, inode->i_generation, inode, count, *ppos);
705
706         /* "If nbyte is 0, read() will return 0 and have no other results."
707          *                      -- Single Unix Spec */
708         if (count == 0)
709                 RETURN(0);
710
711         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
712                             count);
713
714         if (!lsm)
715                 RETURN(0);
716
717         policy.l_extent.start = *ppos;
718         policy.l_extent.end = *ppos + count - 1;
719
720         rc = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh,
721                                 (filp->f_flags & O_NONBLOCK) ?
722                                         LDLM_FL_BLOCK_NOWAIT: 0);
723         if (rc != 0)
724                 RETURN(rc);
725
726         kms = lov_merge_size(lsm, 1);
727         if (*ppos + count - 1 > kms) {
728                 /* A glimpse is necessary to determine whether we return a short
729                  * read or some zeroes at the end of the buffer */
730                 struct ost_lvb lvb;
731                 retval = ll_glimpse_size(inode, &lvb);
732                 if (retval)
733                         goto out;
734                 inode->i_size = lvb.lvb_size;
735         } else {
736                 inode->i_size = kms;
737         }
738
739         CDEBUG(D_INFO, "Read ino %lu, "LPSZ" bytes, offset %lld, i_size %llu\n",
740                inode->i_ino, count, *ppos, inode->i_size);
741
742         /* turn off the kernel's read-ahead */
743 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
744         filp->f_ramax = 0;
745 #else
746         filp->f_ra.ra_pages = 0;
747 #endif
748         retval = generic_file_read(filp, buf, count, ppos);
749
750  out:
751         ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
752         RETURN(retval);
753 }
754
755 /*
756  * Write to a file (through the page cache).
757  */
758 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
759                              loff_t *ppos)
760 {
761         struct ll_file_data *fd = file->private_data;
762         struct inode *inode = file->f_dentry->d_inode;
763         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
764         struct lustre_handle lockh = { 0 };
765         ldlm_policy_data_t policy;
766         loff_t maxbytes = ll_file_maxbytes(inode);
767         ssize_t retval;
768         int nonblock = 0, rc;
769         ENTRY;
770         if (file->f_flags & O_NONBLOCK)
771                 nonblock = LDLM_FL_BLOCK_NOWAIT;
772         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
773                inode->i_ino, inode->i_generation, inode, count, *ppos);
774
775         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
776
777         /* POSIX, but surprised the VFS doesn't check this already */
778         if (count == 0)
779                 RETURN(0);
780
781         /* If file was opened for LL_IOC_LOV_SETSTRIPE but the ioctl wasn't
782          * called on the file, don't fail the below assertion (bug 2388). */
783         if (file->f_flags & O_LOV_DELAY_CREATE && lsm == NULL)
784                 RETURN(-EBADF);
785
786         LASSERT(lsm);
787
788         if (file->f_flags & O_APPEND) {
789                 policy.l_extent.start = 0;
790                 policy.l_extent.end = OBD_OBJECT_EOF;
791         } else  {
792                 policy.l_extent.start = *ppos;
793                 policy.l_extent.end = *ppos + count - 1;
794         }
795
796         rc = ll_extent_lock(fd, inode, lsm, LCK_PW, &policy, &lockh, nonblock);
797         if (rc != 0)
798                 RETURN(rc);
799
800         /* this is ok, g_f_w will overwrite this under i_sem if it races
801          * with a local truncate, it just makes our maxbyte checking easier */
802         if (file->f_flags & O_APPEND)
803                 *ppos = inode->i_size;
804
805         if (*ppos >= maxbytes) {
806                 if (count || *ppos > maxbytes) {
807                         send_sig(SIGXFSZ, current, 0);
808                         GOTO(out, retval = -EFBIG);
809                 }
810         }
811         if (*ppos + count > maxbytes)
812                 count = maxbytes - *ppos;
813
814         CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
815                inode->i_ino, count, *ppos);
816
817         /* generic_file_write handles O_APPEND after getting i_sem */
818         retval = generic_file_write(file, buf, count, ppos);
819
820 out:
821         ll_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
822         lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
823                             retval > 0 ? retval : 0);
824         RETURN(retval);
825 }
826
827 static int ll_lov_recreate_obj(struct inode *inode, struct file *file,
828                                unsigned long arg)
829 {
830         struct ll_inode_info *lli = ll_i2info(inode);
831         struct obd_export *exp = ll_i2obdexp(inode);
832         struct ll_recreate_obj ucreatp;
833         struct obd_trans_info oti = { 0 };
834         struct obdo *oa = NULL;
835         int lsm_size;
836         int rc = 0;
837         struct lov_stripe_md *lsm, *lsm2;
838         ENTRY;
839
840         if (!capable (CAP_SYS_ADMIN))
841                 RETURN(-EPERM);
842
843         rc = copy_from_user(&ucreatp, (struct ll_recreate_obj *)arg, 
844                             sizeof(struct ll_recreate_obj));
845         if (rc) {
846                 RETURN(-EFAULT);
847         }
848         oa = obdo_alloc();
849         if (oa == NULL) {
850                 RETURN(-ENOMEM);
851         }
852
853         down(&lli->lli_open_sem);
854         lsm = lli->lli_smd;
855         if (lsm == NULL) {
856                 up(&lli->lli_open_sem);
857                 obdo_free(oa);
858                 RETURN (-ENOENT);
859         }
860         lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
861                    (lsm->lsm_stripe_count));
862
863         OBD_ALLOC(lsm2, lsm_size);
864         if (lsm2 == NULL) {
865                 up(&lli->lli_open_sem);
866                 obdo_free(oa);
867                 RETURN(-ENOMEM);
868         }
869
870         oa->o_id = ucreatp.lrc_id; 
871         oa->o_nlink = ucreatp.lrc_ost_idx;
872         oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
873         oa->o_flags |= OBD_FL_RECREATE_OBJS;
874         obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
875                                    OBD_MD_FLMTIME | OBD_MD_FLCTIME);
876
877         oti.oti_objid = NULL;
878         memcpy(lsm2, lsm, lsm_size);
879         rc = obd_create(exp, oa, &lsm2, &oti);
880
881         up(&lli->lli_open_sem);
882         OBD_FREE(lsm2, lsm_size);
883         obdo_free(oa);
884         RETURN (rc);
885 }
886
887 static int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
888                                     int flags, struct lov_user_md *lum,
889                                     int lum_size)
890 {
891         struct ll_inode_info *lli = ll_i2info(inode);
892         struct file *f;
893         struct obd_export *exp = ll_i2obdexp(inode);
894         struct lov_stripe_md *lsm;
895         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
896         struct ptlrpc_request *req = NULL;
897         int rc = 0;
898         struct lustre_md md;
899         ENTRY;
900
901         down(&lli->lli_open_sem);
902         lsm = lli->lli_smd;
903         if (lsm) {
904                 up(&lli->lli_open_sem);
905                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
906                        inode->i_ino);
907                 RETURN(-EEXIST);
908         }
909
910         f = get_empty_filp();
911         if (!f)
912                 GOTO(out, -ENOMEM);
913
914         f->f_dentry = file->f_dentry;
915         f->f_vfsmnt = file->f_vfsmnt;
916
917         rc = ll_intent_file_open(f, lum, lum_size, &oit);
918         if (rc)
919                 GOTO(out, rc);
920         if (it_disposition(&oit, DISP_LOOKUP_NEG))
921                 GOTO(out, -ENOENT);
922         req = oit.d.lustre.it_data;
923         rc = oit.d.lustre.it_status;
924
925         if (rc < 0)
926                 GOTO(out, rc);
927
928         rc = mdc_req2lustre_md(req, 1, exp, &md);
929         if (rc)
930                 GOTO(out, rc);
931         ll_update_inode(f->f_dentry->d_inode, md.body, md.lsm);
932
933         rc = ll_local_open(f, &oit);
934         if (rc)
935                 GOTO(out, rc);
936         ll_intent_release(&oit);
937
938         rc = ll_file_release(f->f_dentry->d_inode, f);
939
940  out:
941         if (f)
942                 put_filp(f);
943         up(&lli->lli_open_sem);
944         if (req != NULL)
945                 ptlrpc_req_finished(req);
946         RETURN(rc);
947 }
948
949 static int ll_lov_setea(struct inode *inode, struct file *file,
950                             unsigned long arg)
951 {
952         int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
953         struct lov_user_md  *lump;
954         int lum_size = sizeof(struct lov_user_md) +
955                        sizeof(struct lov_user_ost_data);
956         int rc;
957         ENTRY;
958
959         if (!capable (CAP_SYS_ADMIN))
960                 RETURN(-EPERM);
961
962         OBD_ALLOC(lump, lum_size);
963         if (lump == NULL) {
964                 RETURN(-ENOMEM);
965         }
966         rc = copy_from_user(lump, (struct lov_user_md  *)arg, lum_size);
967         if (rc) {
968                 OBD_FREE(lump, lum_size);
969                 RETURN(-EFAULT);
970         }
971
972         rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
973
974         OBD_FREE(lump, lum_size);
975         RETURN(rc);
976 }
977
978 static int ll_lov_setstripe(struct inode *inode, struct file *file,
979                             unsigned long arg)
980 {
981         struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
982         int rc;
983         int flags = FMODE_WRITE;
984         ENTRY;
985
986         /* Bug 1152: copy properly when this is no longer true */
987         LASSERT(sizeof(lum) == sizeof(*lump));
988         LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
989         rc = copy_from_user(&lum, lump, sizeof(lum));
990         if (rc)
991                 RETURN(-EFAULT);
992
993         rc = ll_lov_setstripe_ea_info(inode, file, flags, &lum, sizeof(lum));
994         RETURN(rc);
995 }
996
997 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
998 {
999         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1000
1001         if (!lsm)
1002                 RETURN(-ENODATA);
1003
1004         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2obdexp(inode), 0, lsm,
1005                             (void *)arg);
1006 }
1007
1008 static int ll_get_grouplock(struct inode *inode, struct file *file,
1009                          unsigned long arg)
1010 {
1011         struct ll_file_data *fd = file->private_data;
1012         ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1013                                                     .end = OBD_OBJECT_EOF}};
1014         struct lustre_handle lockh = { 0 };
1015         struct ll_inode_info *lli = ll_i2info(inode);
1016         struct lov_stripe_md *lsm = lli->lli_smd;
1017         int flags = 0, rc;
1018         ENTRY;
1019
1020         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1021                 RETURN(-EINVAL);
1022         }
1023
1024         policy.l_extent.gid = arg;
1025         if (file->f_flags & O_NONBLOCK)
1026                 flags = LDLM_FL_BLOCK_NOWAIT;
1027
1028         rc = ll_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh, flags);
1029         if (rc != 0)
1030                 RETURN(rc);
1031
1032         fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1033         fd->fd_gid = arg;
1034         memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1035
1036         RETURN(0);
1037 }
1038
1039 static int ll_put_grouplock(struct inode *inode, struct file *file,
1040                          unsigned long arg)
1041 {
1042         struct ll_file_data *fd = file->private_data;
1043         struct ll_inode_info *lli = ll_i2info(inode);
1044         struct lov_stripe_md *lsm = lli->lli_smd;
1045         int rc;
1046         ENTRY;
1047
1048         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1049                 /* Ugh, it's already unlocked. */
1050                 RETURN(-EINVAL);
1051         }
1052
1053         if (fd->fd_gid != arg) /* Ugh? Unlocking with different gid? */
1054                 RETURN(-EINVAL);
1055
1056         fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1057
1058         rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1059         if (rc)
1060                 RETURN(rc);
1061
1062         fd->fd_gid = 0;
1063         memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1064
1065         RETURN(0);
1066 }
1067
1068 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1069                   unsigned long arg)
1070 {
1071         struct ll_file_data *fd = file->private_data;
1072         int flags;
1073         ENTRY;
1074
1075         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1076                inode->i_generation, inode, cmd);
1077
1078         if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
1079                 RETURN(-ENOTTY);
1080
1081         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
1082         switch(cmd) {
1083         case LL_IOC_GETFLAGS:
1084                 /* Get the current value of the file flags */
1085                 return put_user(fd->fd_flags, (int *)arg);
1086         case LL_IOC_SETFLAGS:
1087         case LL_IOC_CLRFLAGS:
1088                 /* Set or clear specific file flags */
1089                 /* XXX This probably needs checks to ensure the flags are
1090                  *     not abused, and to handle any flag side effects.
1091                  */
1092                 if (get_user(flags, (int *) arg))
1093                         RETURN(-EFAULT);
1094
1095                 if (cmd == LL_IOC_SETFLAGS)
1096                         fd->fd_flags |= flags;
1097                 else
1098                         fd->fd_flags &= ~flags;
1099                 RETURN(0);
1100         case LL_IOC_LOV_SETSTRIPE:
1101                 RETURN(ll_lov_setstripe(inode, file, arg));
1102         case LL_IOC_LOV_SETEA:
1103                 RETURN(ll_lov_setea(inode, file, arg));
1104         case LL_IOC_LOV_GETSTRIPE:
1105                 RETURN(ll_lov_getstripe(inode, arg));
1106         case LL_IOC_RECREATE_OBJ:
1107                 RETURN(ll_lov_recreate_obj(inode, file, arg));
1108         case EXT3_IOC_GETFLAGS:
1109         case EXT3_IOC_SETFLAGS:
1110                 RETURN( ll_iocontrol(inode, file, cmd, arg) );
1111         case LL_IOC_GROUP_LOCK:
1112                 RETURN(ll_get_grouplock(inode, file, arg));
1113         case LL_IOC_GROUP_UNLOCK:
1114                 RETURN(ll_put_grouplock(inode, file, arg));
1115         /* We need to special case any other ioctls we want to handle,
1116          * to send them to the MDS/OST as appropriate and to properly
1117          * network encode the arg field.
1118         case EXT2_IOC_GETVERSION_OLD:
1119         case EXT2_IOC_GETVERSION_NEW:
1120         case EXT2_IOC_SETVERSION_OLD:
1121         case EXT2_IOC_SETVERSION_NEW:
1122         */
1123         default:
1124                 RETURN( obd_iocontrol(cmd, ll_i2obdexp(inode), 0, NULL,
1125                                       (void *)arg) );
1126         }
1127 }
1128
1129 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1130 {
1131         struct inode *inode = file->f_dentry->d_inode;
1132         struct ll_file_data *fd = file->private_data;
1133         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1134         struct lustre_handle lockh = {0};
1135         loff_t retval;
1136         ENTRY;
1137         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
1138                inode->i_generation, inode,
1139                offset + ((origin==2) ? inode->i_size : file->f_pos));
1140
1141         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
1142         if (origin == 2) { /* SEEK_END */
1143                 int nonblock = 0, rc;
1144                 ldlm_policy_data_t policy = { .l_extent = {0, OBD_OBJECT_EOF }};
1145
1146                 if (file->f_flags & O_NONBLOCK)
1147                         nonblock = LDLM_FL_BLOCK_NOWAIT;
1148
1149                 rc = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh,
1150                                      nonblock);
1151                 if (rc != 0)
1152                         RETURN(rc);
1153
1154                 offset += inode->i_size;
1155         } else if (origin == 1) { /* SEEK_CUR */
1156                 offset += file->f_pos;
1157         }
1158
1159         retval = -EINVAL;
1160         if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1161                 if (offset != file->f_pos) {
1162                         file->f_pos = offset;
1163 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1164                         file->f_reada = 0;
1165                         file->f_version = ++event;
1166 #endif
1167                 }
1168                 retval = offset;
1169         }
1170
1171         if (origin == 2)
1172                 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
1173         RETURN(retval);
1174 }
1175
1176 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1177 {
1178         struct inode *inode = dentry->d_inode;
1179         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1180         struct ll_fid fid;
1181         struct ptlrpc_request *req;
1182         int rc, err;
1183         ENTRY;
1184         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1185                inode->i_generation, inode);
1186
1187         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
1188
1189         /* fsync's caller has already called _fdata{sync,write}, we want
1190          * that IO to finish before calling the osc and mdc sync methods */
1191         rc = filemap_fdatawait(inode->i_mapping);
1192
1193         ll_inode2fid(&fid, inode);
1194         err = mdc_sync(ll_i2sbi(inode)->ll_mdc_exp, &fid, &req);
1195         if (!rc)
1196                 rc = err;
1197         if (!err)
1198                 ptlrpc_req_finished(req);
1199
1200         if (data && lsm) {
1201                 struct obdo *oa = obdo_alloc();
1202
1203                 if (!oa)
1204                         RETURN(rc ? rc : -ENOMEM);
1205
1206                 oa->o_id = lsm->lsm_object_id;
1207                 oa->o_valid = OBD_MD_FLID;
1208                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1209                                            OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1210
1211                 err = obd_sync(ll_i2sbi(inode)->ll_osc_exp, oa, lsm,
1212                                0, OBD_OBJECT_EOF);
1213                 if (!rc)
1214                         rc = err;
1215                 obdo_free(oa);
1216         }
1217
1218         RETURN(rc);
1219 }
1220
1221 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
1222 {
1223         struct inode *inode = file->f_dentry->d_inode;
1224         struct ll_sb_info *sbi = ll_i2sbi(inode);
1225         struct obd_device *obddev;
1226         struct ldlm_res_id res_id =
1227                     { .name = {inode->i_ino, inode->i_generation, LDLM_FLOCK} };
1228         struct lustre_handle lockh = {0};
1229         ldlm_policy_data_t flock;
1230         ldlm_mode_t mode = 0;
1231         int flags = 0;
1232         int rc;
1233         ENTRY;
1234
1235         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
1236                inode->i_ino, file_lock);
1237
1238         flock.l_flock.pid = file_lock->fl_pid;
1239         flock.l_flock.start = file_lock->fl_start;
1240         flock.l_flock.end = file_lock->fl_end;
1241
1242         switch (file_lock->fl_type) {
1243         case F_RDLCK:
1244                 mode = LCK_PR;
1245                 break;
1246         case F_UNLCK:
1247                 /* An unlock request may or may not have any relation to
1248                  * existing locks so we may not be able to pass a lock handle
1249                  * via a normal ldlm_lock_cancel() request. The request may even
1250                  * unlock a byte range in the middle of an existing lock. In
1251                  * order to process an unlock request we need all of the same
1252                  * information that is given with a normal read or write record
1253                  * lock request. To avoid creating another ldlm unlock (cancel)
1254                  * message we'll treat a LCK_NL flock request as an unlock. */
1255                 mode = LCK_NL;
1256                 break;
1257         case F_WRLCK:
1258                 mode = LCK_PW;
1259                 break;
1260         default:
1261                 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1262                 LBUG();
1263         }
1264
1265         switch (cmd) {
1266         case F_SETLKW:
1267                 flags = 0;
1268                 break;
1269         case F_SETLK:
1270                 flags = LDLM_FL_BLOCK_NOWAIT;
1271                 break;
1272         case F_GETLK:
1273                 flags = LDLM_FL_TEST_LOCK;
1274                 /* Save the old mode so that if the mode in the lock changes we
1275                  * can decrement the appropriate reader or writer refcount. */
1276                 file_lock->fl_type = mode;
1277                 break;
1278         default:
1279                 CERROR("unknown fcntl lock command: %d\n", cmd);
1280                 LBUG();
1281         }
1282
1283         CDEBUG(D_DLMTRACE, "inode=%lu, pid="LPU64", flags=%#x, mode=%u, "
1284                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
1285                flags, mode, flock.l_flock.start, flock.l_flock.end);
1286
1287         obddev = sbi->ll_mdc_exp->exp_obd;
1288         rc = ldlm_cli_enqueue(sbi->ll_mdc_exp, NULL, obddev->obd_namespace,
1289                               res_id, LDLM_FLOCK, &flock, mode, &flags,
1290                               NULL, ldlm_flock_completion_ast, NULL, file_lock,
1291                               NULL, 0, NULL, &lockh);
1292         RETURN(rc);
1293 }
1294
1295 static int ll_have_md_lock(struct dentry *de, __u64 lockpart)
1296 {
1297         struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
1298         struct lustre_handle lockh;
1299         struct ldlm_res_id res_id = { .name = {0} };
1300         struct obd_device *obddev;
1301         int flags;
1302         ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
1303         ENTRY;
1304
1305         if (!de->d_inode)
1306                RETURN(0);
1307
1308         obddev = sbi->ll_mdc_exp->exp_obd;
1309         res_id.name[0] = de->d_inode->i_ino;
1310         res_id.name[1] = de->d_inode->i_generation;
1311
1312         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1313
1314         /* FIXME use LDLM_FL_TEST_LOCK instead */
1315         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1316         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
1317                             &policy, LCK_PR, &lockh)) {
1318                 ldlm_lock_decref(&lockh, LCK_PR);
1319                 RETURN(1);
1320         }
1321
1322         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
1323                             &policy, LCK_PW, &lockh)) {
1324                 ldlm_lock_decref(&lockh, LCK_PW);
1325                 RETURN(1);
1326         }
1327         RETURN(0);
1328 }
1329
1330 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
1331 {
1332         struct inode *inode = dentry->d_inode;
1333         struct ll_inode_info *lli;
1334         struct lov_stripe_md *lsm;
1335         int rc;
1336         ENTRY;
1337
1338         if (!inode) {
1339                 CERROR("REPORT THIS LINE TO PETER\n");
1340                 RETURN(0);
1341         }
1342         lli = ll_i2info(inode);
1343         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
1344                inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
1345 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1346         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1347 #endif
1348
1349         if (!ll_have_md_lock(dentry, MDS_INODELOCK_UPDATE)) {
1350                 struct ptlrpc_request *req = NULL;
1351                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
1352                 struct ll_fid fid;
1353                 unsigned long valid = 0;
1354                 int ealen = 0;
1355
1356                 if (S_ISREG(inode->i_mode)) {
1357                         ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
1358                         valid |= OBD_MD_FLEASIZE;
1359                 }
1360                 ll_inode2fid(&fid, inode);
1361                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
1362                 if (rc) {
1363                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1364                         RETURN(-abs(rc));
1365                 }
1366                 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, 0, NULL);
1367                 if (rc) {
1368                         ptlrpc_req_finished(req);
1369                         RETURN(rc);
1370                 }
1371                 ptlrpc_req_finished(req);
1372         }
1373
1374         lsm = lli->lli_smd;
1375         if (lsm == NULL) /* object not yet allocated, don't validate size */
1376                 RETURN(0);
1377
1378         /* ll_glimpse_size will prefer locally cached writes if they extend
1379          * the file */
1380         {
1381                 struct ost_lvb lvb;
1382
1383                 rc = ll_glimpse_size(inode, &lvb);
1384                 inode->i_size = lvb.lvb_size;
1385         }
1386         RETURN(rc);
1387 }
1388
1389 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1390 int ll_getattr(struct vfsmount *mnt, struct dentry *de,
1391                struct lookup_intent *it, struct kstat *stat)
1392 {
1393         int res = 0;
1394         struct inode *inode = de->d_inode;
1395
1396         res = ll_inode_revalidate_it(de, it);
1397         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1398
1399         if (res)
1400                 return res;
1401
1402         stat->dev = inode->i_sb->s_dev;
1403         stat->ino = inode->i_ino;
1404         stat->mode = inode->i_mode;
1405         stat->nlink = inode->i_nlink;
1406         stat->uid = inode->i_uid;
1407         stat->gid = inode->i_gid;
1408         stat->rdev = kdev_t_to_nr(inode->i_rdev);
1409         stat->atime = inode->i_atime;
1410         stat->mtime = inode->i_mtime;
1411         stat->ctime = inode->i_ctime;
1412         stat->size = inode->i_size;
1413         stat->blksize = inode->i_blksize;
1414         stat->blocks = inode->i_blocks;
1415         return 0;
1416 }
1417 #endif
1418
1419 struct file_operations ll_file_operations = {
1420         .read           = ll_file_read,
1421         .write          = ll_file_write,
1422         .ioctl          = ll_file_ioctl,
1423         .open           = ll_file_open,
1424         .release        = ll_file_release,
1425         .mmap           = generic_file_mmap,
1426         .llseek         = ll_file_seek,
1427 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1428         .sendfile       = generic_file_sendfile,
1429 #endif
1430         .fsync          = ll_fsync,
1431         //.lock           ll_file_flock
1432 };
1433
1434 struct inode_operations ll_file_inode_operations = {
1435         .setattr_raw    = ll_setattr_raw,
1436         .setattr        = ll_setattr,
1437         .truncate       = ll_truncate,
1438 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1439         .getattr_it     = ll_getattr,
1440 #else
1441         .revalidate_it  = ll_inode_revalidate_it,
1442 #endif
1443 };
1444