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