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