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