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