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