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