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