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