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