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