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