Whamcloud - gitweb
b=24037 avoid conflicting DCACHE_LUSTRE_INVALID
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/file.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <lustre_dlm.h>
45 #include <lustre_lite.h>
46 #include <lustre_mdc.h>
47 #include <linux/pagemap.h>
48 #include <linux/file.h>
49 #include "llite_internal.h"
50 #include <lustre/ll_fiemap.h>
51
52 #include "cl_object.h"
53
54 struct ll_file_data *ll_file_data_get(void)
55 {
56         struct ll_file_data *fd;
57
58         OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, CFS_ALLOC_IO);
59         return fd;
60 }
61
62 static void ll_file_data_put(struct ll_file_data *fd)
63 {
64         if (fd != NULL)
65                 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
66 }
67
68 void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
69                           struct lustre_handle *fh)
70 {
71         op_data->op_fid1 = ll_i2info(inode)->lli_fid;
72         op_data->op_attr.ia_mode = inode->i_mode;
73         op_data->op_attr.ia_atime = inode->i_atime;
74         op_data->op_attr.ia_mtime = inode->i_mtime;
75         op_data->op_attr.ia_ctime = inode->i_ctime;
76         op_data->op_attr.ia_size = i_size_read(inode);
77         op_data->op_attr_blocks = inode->i_blocks;
78         ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
79                                         ll_inode_to_ext_flags(inode->i_flags);
80         op_data->op_ioepoch = ll_i2info(inode)->lli_ioepoch;
81         if (fh)
82                 op_data->op_handle = *fh;
83         op_data->op_capa1 = ll_mdscapa_get(inode);
84 }
85
86 /**
87  * Closes the IO epoch and packs all the attributes into @op_data for
88  * the CLOSE rpc.
89  */
90 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
91                              struct obd_client_handle *och)
92 {
93         ENTRY;
94
95         op_data->op_attr.ia_valid = ATTR_MODE | ATTR_ATIME_SET |
96                                  ATTR_MTIME_SET | ATTR_CTIME_SET;
97
98         if (!(och->och_flags & FMODE_WRITE))
99                 goto out;
100
101         if (!exp_connect_som(ll_i2mdexp(inode)) || !S_ISREG(inode->i_mode))
102                 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
103         else
104                 ll_ioepoch_close(inode, op_data, &och, 0);
105
106 out:
107         ll_pack_inode2opdata(inode, op_data, &och->och_fh);
108         ll_prep_md_op_data(op_data, inode, NULL, NULL,
109                            0, 0, LUSTRE_OPC_ANY, NULL);
110         EXIT;
111 }
112
113 static int ll_close_inode_openhandle(struct obd_export *md_exp,
114                                      struct inode *inode,
115                                      struct obd_client_handle *och)
116 {
117         struct obd_export *exp = ll_i2mdexp(inode);
118         struct md_op_data *op_data;
119         struct ptlrpc_request *req = NULL;
120         struct obd_device *obd = class_exp2obd(exp);
121         int epoch_close = 1;
122         int rc;
123         ENTRY;
124
125         if (obd == NULL) {
126                 /*
127                  * XXX: in case of LMV, is this correct to access
128                  * ->exp_handle?
129                  */
130                 CERROR("Invalid MDC connection handle "LPX64"\n",
131                        ll_i2mdexp(inode)->exp_handle.h_cookie);
132                 GOTO(out, rc = 0);
133         }
134
135         OBD_ALLOC_PTR(op_data);
136         if (op_data == NULL)
137                 GOTO(out, rc = -ENOMEM); // XXX We leak openhandle and request here.
138
139         ll_prepare_close(inode, op_data, och);
140         epoch_close = (op_data->op_flags & MF_EPOCH_CLOSE);
141         rc = md_close(md_exp, op_data, och->och_mod, &req);
142         if (rc == -EAGAIN) {
143                 /* This close must have the epoch closed. */
144                 LASSERT(epoch_close);
145                 /* MDS has instructed us to obtain Size-on-MDS attribute from
146                  * OSTs and send setattr to back to MDS. */
147                 rc = ll_som_update(inode, op_data);
148                 if (rc) {
149                         CERROR("inode %lu mdc Size-on-MDS update failed: "
150                                "rc = %d\n", inode->i_ino, rc);
151                         rc = 0;
152                 }
153         } else if (rc) {
154                 CERROR("inode %lu mdc close failed: rc = %d\n",
155                        inode->i_ino, rc);
156         }
157         ll_finish_md_op_data(op_data);
158
159         if (rc == 0) {
160                 rc = ll_objects_destroy(req, inode);
161                 if (rc)
162                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
163                                inode->i_ino, rc);
164         }
165
166         EXIT;
167 out:
168
169         if (exp_connect_som(exp) && !epoch_close &&
170             S_ISREG(inode->i_mode) && (och->och_flags & FMODE_WRITE)) {
171                 ll_queue_done_writing(inode, LLIF_DONE_WRITING);
172         } else {
173                 md_clear_open_replay_data(md_exp, och);
174                 /* Free @och if it is not waiting for DONE_WRITING. */
175                 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
176                 OBD_FREE_PTR(och);
177         }
178         if (req) /* This is close request */
179                 ptlrpc_req_finished(req);
180         return rc;
181 }
182
183 int ll_md_real_close(struct inode *inode, int flags)
184 {
185         struct ll_inode_info *lli = ll_i2info(inode);
186         struct obd_client_handle **och_p;
187         struct obd_client_handle *och;
188         __u64 *och_usecount;
189         int rc = 0;
190         ENTRY;
191
192         if (flags & FMODE_WRITE) {
193                 och_p = &lli->lli_mds_write_och;
194                 och_usecount = &lli->lli_open_fd_write_count;
195         } else if (flags & FMODE_EXEC) {
196                 och_p = &lli->lli_mds_exec_och;
197                 och_usecount = &lli->lli_open_fd_exec_count;
198         } else {
199                 LASSERT(flags & FMODE_READ);
200                 och_p = &lli->lli_mds_read_och;
201                 och_usecount = &lli->lli_open_fd_read_count;
202         }
203
204         cfs_down(&lli->lli_och_sem);
205         if (*och_usecount) { /* There are still users of this handle, so
206                                 skip freeing it. */
207                 cfs_up(&lli->lli_och_sem);
208                 RETURN(0);
209         }
210         och=*och_p;
211         *och_p = NULL;
212         cfs_up(&lli->lli_och_sem);
213
214         if (och) { /* There might be a race and somebody have freed this och
215                       already */
216                 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
217                                                inode, och);
218         }
219
220         RETURN(rc);
221 }
222
223 int ll_md_close(struct obd_export *md_exp, struct inode *inode,
224                 struct file *file)
225 {
226         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
227         struct ll_inode_info *lli = ll_i2info(inode);
228         int rc = 0;
229         ENTRY;
230
231         /* clear group lock, if present */
232         if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
233                 ll_put_grouplock(inode, file, fd->fd_grouplock.cg_gid);
234
235         /* Let's see if we have good enough OPEN lock on the file and if
236            we can skip talking to MDS */
237         if (file->f_dentry->d_inode) { /* Can this ever be false? */
238                 int lockmode;
239                 int flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
240                 struct lustre_handle lockh;
241                 struct inode *inode = file->f_dentry->d_inode;
242                 ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_OPEN}};
243
244                 cfs_down(&lli->lli_och_sem);
245                 if (fd->fd_omode & FMODE_WRITE) {
246                         lockmode = LCK_CW;
247                         LASSERT(lli->lli_open_fd_write_count);
248                         lli->lli_open_fd_write_count--;
249                 } else if (fd->fd_omode & FMODE_EXEC) {
250                         lockmode = LCK_PR;
251                         LASSERT(lli->lli_open_fd_exec_count);
252                         lli->lli_open_fd_exec_count--;
253                 } else {
254                         lockmode = LCK_CR;
255                         LASSERT(lli->lli_open_fd_read_count);
256                         lli->lli_open_fd_read_count--;
257                 }
258                 cfs_up(&lli->lli_och_sem);
259
260                 if (!md_lock_match(md_exp, flags, ll_inode2fid(inode),
261                                    LDLM_IBITS, &policy, lockmode,
262                                    &lockh)) {
263                         rc = ll_md_real_close(file->f_dentry->d_inode,
264                                               fd->fd_omode);
265                 }
266         } else {
267                 CERROR("Releasing a file %p with negative dentry %p. Name %s",
268                        file, file->f_dentry, file->f_dentry->d_name.name);
269         }
270
271         LUSTRE_FPRIVATE(file) = NULL;
272         ll_file_data_put(fd);
273         ll_capa_close(inode);
274
275         RETURN(rc);
276 }
277
278 int lov_test_and_clear_async_rc(struct lov_stripe_md *lsm);
279
280 /* While this returns an error code, fput() the caller does not, so we need
281  * to make every effort to clean up all of our state here.  Also, applications
282  * rarely check close errors and even if an error is returned they will not
283  * re-try the close call.
284  */
285 int ll_file_release(struct inode *inode, struct file *file)
286 {
287         struct ll_file_data *fd;
288         struct ll_sb_info *sbi = ll_i2sbi(inode);
289         struct ll_inode_info *lli = ll_i2info(inode);
290         struct lov_stripe_md *lsm = lli->lli_smd;
291         int rc;
292         ENTRY;
293
294         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
295                inode->i_generation, inode);
296
297 #ifdef CONFIG_FS_POSIX_ACL
298         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
299             inode == inode->i_sb->s_root->d_inode) {
300                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
301
302                 LASSERT(fd != NULL);
303                 if (unlikely(fd->fd_flags & LL_FILE_RMTACL)) {
304                         fd->fd_flags &= ~LL_FILE_RMTACL;
305                         rct_del(&sbi->ll_rct, cfs_curproc_pid());
306                         et_search_free(&sbi->ll_et, cfs_curproc_pid());
307                 }
308         }
309 #endif
310
311         if (inode->i_sb->s_root != file->f_dentry)
312                 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
313         fd = LUSTRE_FPRIVATE(file);
314         LASSERT(fd != NULL);
315
316         /* The last ref on @file, maybe not the the owner pid of statahead.
317          * Different processes can open the same dir, "ll_opendir_key" means:
318          * it is me that should stop the statahead thread. */
319         if (lli->lli_opendir_key == fd && lli->lli_opendir_pid != 0)
320                 ll_stop_statahead(inode, lli->lli_opendir_key);
321
322         if (inode->i_sb->s_root == file->f_dentry) {
323                 LUSTRE_FPRIVATE(file) = NULL;
324                 ll_file_data_put(fd);
325                 RETURN(0);
326         }
327
328         if (lsm)
329                 lov_test_and_clear_async_rc(lsm);
330         lli->lli_async_rc = 0;
331
332         rc = ll_md_close(sbi->ll_md_exp, inode, file);
333
334         if (OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, obd_fail_val))
335                 libcfs_debug_dumplog();
336
337         RETURN(rc);
338 }
339
340 static int ll_intent_file_open(struct file *file, void *lmm,
341                                int lmmsize, struct lookup_intent *itp)
342 {
343         struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
344         struct dentry *parent = file->f_dentry->d_parent;
345         const char *name = file->f_dentry->d_name.name;
346         const int len = file->f_dentry->d_name.len;
347         struct md_op_data *op_data;
348         struct ptlrpc_request *req;
349         int rc;
350         ENTRY;
351
352         if (!parent)
353                 RETURN(-ENOENT);
354
355         /* Usually we come here only for NFSD, and we want open lock.
356            But we can also get here with pre 2.6.15 patchless kernels, and in
357            that case that lock is also ok */
358         /* We can also get here if there was cached open handle in revalidate_it
359          * but it disappeared while we were getting from there to ll_file_open.
360          * But this means this file was closed and immediatelly opened which
361          * makes a good candidate for using OPEN lock */
362         /* If lmmsize & lmm are not 0, we are just setting stripe info
363          * parameters. No need for the open lock */
364         if (!lmm && !lmmsize)
365                 itp->it_flags |= MDS_OPEN_LOCK;
366
367         op_data  = ll_prep_md_op_data(NULL, parent->d_inode,
368                                       file->f_dentry->d_inode, name, len,
369                                       O_RDWR, LUSTRE_OPC_ANY, NULL);
370         if (IS_ERR(op_data))
371                 RETURN(PTR_ERR(op_data));
372
373         rc = md_intent_lock(sbi->ll_md_exp, op_data, lmm, lmmsize, itp,
374                             0 /*unused */, &req, ll_md_blocking_ast, 0);
375         ll_finish_md_op_data(op_data);
376         if (rc == -ESTALE) {
377                 /* reason for keep own exit path - don`t flood log
378                 * with messages with -ESTALE errors.
379                 */
380                 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
381                      it_open_error(DISP_OPEN_OPEN, itp))
382                         GOTO(out, rc);
383                 ll_release_openhandle(file->f_dentry, itp);
384                 GOTO(out, rc);
385         }
386
387         if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
388                 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
389                 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
390                 GOTO(out, rc);
391         }
392
393         rc = ll_prep_inode(&file->f_dentry->d_inode, req, NULL);
394         if (!rc && itp->d.lustre.it_lock_mode)
395                 md_set_lock_data(sbi->ll_md_exp,
396                                  &itp->d.lustre.it_lock_handle,
397                                  file->f_dentry->d_inode, NULL);
398
399 out:
400         ptlrpc_req_finished(itp->d.lustre.it_data);
401         it_clear_disposition(itp, DISP_ENQ_COMPLETE);
402         ll_intent_drop_lock(itp);
403
404         RETURN(rc);
405 }
406
407 /**
408  * Assign an obtained @ioepoch to client's inode. No lock is needed, MDS does
409  * not believe attributes if a few ioepoch holders exist. Attributes for
410  * previous ioepoch if new one is opened are also skipped by MDS.
411  */
412 void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch)
413 {
414         if (ioepoch && lli->lli_ioepoch != ioepoch) {
415                 lli->lli_ioepoch = ioepoch;
416                 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID"\n",
417                        ioepoch, PFID(&lli->lli_fid));
418         }
419 }
420
421 static int ll_och_fill(struct obd_export *md_exp, struct ll_inode_info *lli,
422                        struct lookup_intent *it, struct obd_client_handle *och)
423 {
424         struct ptlrpc_request *req = it->d.lustre.it_data;
425         struct mdt_body *body;
426
427         LASSERT(och);
428
429         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
430         LASSERT(body != NULL);                      /* reply already checked out */
431
432         memcpy(&och->och_fh, &body->handle, sizeof(body->handle));
433         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
434         och->och_fid = lli->lli_fid;
435         och->och_flags = it->it_flags;
436         ll_ioepoch_open(lli, body->ioepoch);
437
438         return md_set_open_replay_data(md_exp, och, req);
439 }
440
441 int ll_local_open(struct file *file, struct lookup_intent *it,
442                   struct ll_file_data *fd, struct obd_client_handle *och)
443 {
444         struct inode *inode = file->f_dentry->d_inode;
445         struct ll_inode_info *lli = ll_i2info(inode);
446         ENTRY;
447
448         LASSERT(!LUSTRE_FPRIVATE(file));
449
450         LASSERT(fd != NULL);
451
452         if (och) {
453                 struct ptlrpc_request *req = it->d.lustre.it_data;
454                 struct mdt_body *body;
455                 int rc;
456
457                 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, lli, it, och);
458                 if (rc)
459                         RETURN(rc);
460
461                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
462                 if ((it->it_flags & FMODE_WRITE) &&
463                     (body->valid & OBD_MD_FLSIZE))
464                         CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID"\n",
465                                lli->lli_ioepoch, PFID(&lli->lli_fid));
466         }
467
468         LUSTRE_FPRIVATE(file) = fd;
469         ll_readahead_init(inode, &fd->fd_ras);
470         fd->fd_omode = it->it_flags;
471         RETURN(0);
472 }
473
474 /* Open a file, and (for the very first open) create objects on the OSTs at
475  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
476  * creation or open until ll_lov_setstripe() ioctl is called.  We grab
477  * lli_open_sem to ensure no other process will create objects, send the
478  * stripe MD to the MDS, or try to destroy the objects if that fails.
479  *
480  * If we already have the stripe MD locally then we don't request it in
481  * md_open(), by passing a lmm_size = 0.
482  *
483  * It is up to the application to ensure no other processes open this file
484  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
485  * used.  We might be able to avoid races of that sort by getting lli_open_sem
486  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
487  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
488  */
489 int ll_file_open(struct inode *inode, struct file *file)
490 {
491         struct ll_inode_info *lli = ll_i2info(inode);
492         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
493                                           .it_flags = file->f_flags };
494         struct lov_stripe_md *lsm;
495         struct ptlrpc_request *req = NULL;
496         struct obd_client_handle **och_p;
497         __u64 *och_usecount;
498         struct ll_file_data *fd;
499         int rc = 0, opendir_set = 0;
500         ENTRY;
501
502         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), flags %o\n", inode->i_ino,
503                inode->i_generation, inode, file->f_flags);
504
505 #ifdef HAVE_VFS_INTENT_PATCHES
506         it = file->f_it;
507 #else
508         it = file->private_data; /* XXX: compat macro */
509         file->private_data = NULL; /* prevent ll_local_open assertion */
510 #endif
511
512         fd = ll_file_data_get();
513         if (fd == NULL)
514                 RETURN(-ENOMEM);
515
516         fd->fd_file = file;
517         if (S_ISDIR(inode->i_mode)) {
518                 cfs_spin_lock(&lli->lli_sa_lock);
519                 if (lli->lli_opendir_key == NULL && lli->lli_opendir_pid == 0) {
520                         LASSERT(lli->lli_sai == NULL);
521                         lli->lli_opendir_key = fd;
522                         lli->lli_opendir_pid = cfs_curproc_pid();
523                         opendir_set = 1;
524                 }
525                 cfs_spin_unlock(&lli->lli_sa_lock);
526         }
527
528         if (inode->i_sb->s_root == file->f_dentry) {
529                 LUSTRE_FPRIVATE(file) = fd;
530                 RETURN(0);
531         }
532
533         if (!it || !it->d.lustre.it_disposition) {
534                 /* Convert f_flags into access mode. We cannot use file->f_mode,
535                  * because everything but O_ACCMODE mask was stripped from
536                  * there */
537                 if ((oit.it_flags + 1) & O_ACCMODE)
538                         oit.it_flags++;
539                 if (file->f_flags & O_TRUNC)
540                         oit.it_flags |= FMODE_WRITE;
541
542                 /* kernel only call f_op->open in dentry_open.  filp_open calls
543                  * dentry_open after call to open_namei that checks permissions.
544                  * Only nfsd_open call dentry_open directly without checking
545                  * permissions and because of that this code below is safe. */
546                 if (oit.it_flags & FMODE_WRITE)
547                         oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
548
549                 /* We do not want O_EXCL here, presumably we opened the file
550                  * already? XXX - NFS implications? */
551                 oit.it_flags &= ~O_EXCL;
552
553                 /* bug20584, if "it_flags" contains O_CREAT, the file will be
554                  * created if necessary, then "IT_CREAT" should be set to keep
555                  * consistent with it */
556                 if (oit.it_flags & O_CREAT)
557                         oit.it_op |= IT_CREAT;
558
559                 it = &oit;
560         }
561
562 restart:
563         /* Let's see if we have file open on MDS already. */
564         if (it->it_flags & FMODE_WRITE) {
565                 och_p = &lli->lli_mds_write_och;
566                 och_usecount = &lli->lli_open_fd_write_count;
567         } else if (it->it_flags & FMODE_EXEC) {
568                 och_p = &lli->lli_mds_exec_och;
569                 och_usecount = &lli->lli_open_fd_exec_count;
570          } else {
571                 och_p = &lli->lli_mds_read_och;
572                 och_usecount = &lli->lli_open_fd_read_count;
573         }
574
575         cfs_down(&lli->lli_och_sem);
576         if (*och_p) { /* Open handle is present */
577                 if (it_disposition(it, DISP_OPEN_OPEN)) {
578                         /* Well, there's extra open request that we do not need,
579                            let's close it somehow. This will decref request. */
580                         rc = it_open_error(DISP_OPEN_OPEN, it);
581                         if (rc) {
582                                 cfs_up(&lli->lli_och_sem);
583                                 ll_file_data_put(fd);
584                                 GOTO(out_openerr, rc);
585                         }
586                         ll_release_openhandle(file->f_dentry, it);
587                         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
588                                              LPROC_LL_OPEN);
589                 }
590                 (*och_usecount)++;
591
592                 rc = ll_local_open(file, it, fd, NULL);
593                 if (rc) {
594                         (*och_usecount)--;
595                         cfs_up(&lli->lli_och_sem);
596                         ll_file_data_put(fd);
597                         GOTO(out_openerr, rc);
598                 }
599         } else {
600                 LASSERT(*och_usecount == 0);
601                 if (!it->d.lustre.it_disposition) {
602                         /* We cannot just request lock handle now, new ELC code
603                            means that one of other OPEN locks for this file
604                            could be cancelled, and since blocking ast handler
605                            would attempt to grab och_sem as well, that would
606                            result in a deadlock */
607                         cfs_up(&lli->lli_och_sem);
608                         it->it_create_mode |= M_CHECK_STALE;
609                         rc = ll_intent_file_open(file, NULL, 0, it);
610                         it->it_create_mode &= ~M_CHECK_STALE;
611                         if (rc) {
612                                 ll_file_data_put(fd);
613                                 GOTO(out_openerr, rc);
614                         }
615
616                         /* Got some error? Release the request */
617                         if (it->d.lustre.it_status < 0) {
618                                 req = it->d.lustre.it_data;
619                                 ptlrpc_req_finished(req);
620                         }
621                         goto restart;
622                 }
623                 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
624                 if (!*och_p) {
625                         ll_file_data_put(fd);
626                         GOTO(out_och_free, rc = -ENOMEM);
627                 }
628                 (*och_usecount)++;
629                 req = it->d.lustre.it_data;
630
631                 /* md_intent_lock() didn't get a request ref if there was an
632                  * open error, so don't do cleanup on the request here
633                  * (bug 3430) */
634                 /* XXX (green): Should not we bail out on any error here, not
635                  * just open error? */
636                 rc = it_open_error(DISP_OPEN_OPEN, it);
637                 if (rc) {
638                         ll_file_data_put(fd);
639                         GOTO(out_och_free, rc);
640                 }
641
642                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
643                 rc = ll_local_open(file, it, fd, *och_p);
644                 if (rc) {
645                         ll_file_data_put(fd);
646                         GOTO(out_och_free, rc);
647                 }
648         }
649         cfs_up(&lli->lli_och_sem);
650
651         /* Must do this outside lli_och_sem lock to prevent deadlock where
652            different kind of OPEN lock for this same inode gets cancelled
653            by ldlm_cancel_lru */
654         if (!S_ISREG(inode->i_mode))
655                 GOTO(out, rc);
656
657         ll_capa_open(inode);
658
659         lsm = lli->lli_smd;
660         if (lsm == NULL) {
661                 if (file->f_flags & O_LOV_DELAY_CREATE ||
662                     !(file->f_mode & FMODE_WRITE)) {
663                         CDEBUG(D_INODE, "object creation was delayed\n");
664                         GOTO(out, rc);
665                 }
666         }
667         file->f_flags &= ~O_LOV_DELAY_CREATE;
668         GOTO(out, rc);
669 out:
670         ptlrpc_req_finished(req);
671         if (req)
672                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
673 out_och_free:
674         if (rc) {
675                 if (*och_p) {
676                         OBD_FREE(*och_p, sizeof (struct obd_client_handle));
677                         *och_p = NULL; /* OBD_FREE writes some magic there */
678                         (*och_usecount)--;
679                 }
680                 cfs_up(&lli->lli_och_sem);
681 out_openerr:
682                 if (opendir_set != 0)
683                         ll_stop_statahead(inode, lli->lli_opendir_key);
684         }
685
686         return rc;
687 }
688
689 /* Fills the obdo with the attributes for the lsm */
690 static int ll_lsm_getattr(struct lov_stripe_md *lsm, struct obd_export *exp,
691                           struct obd_capa *capa, struct obdo *obdo,
692                           __u64 ioepoch, int sync)
693 {
694         struct ptlrpc_request_set *set;
695         struct obd_info            oinfo = { { { 0 } } };
696         int                        rc;
697
698         ENTRY;
699
700         LASSERT(lsm != NULL);
701
702         oinfo.oi_md = lsm;
703         oinfo.oi_oa = obdo;
704         oinfo.oi_oa->o_id = lsm->lsm_object_id;
705         oinfo.oi_oa->o_seq = lsm->lsm_object_seq;
706         oinfo.oi_oa->o_mode = S_IFREG;
707         oinfo.oi_oa->o_ioepoch = ioepoch;
708         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
709                                OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
710                                OBD_MD_FLBLKSZ | OBD_MD_FLATIME |
711                                OBD_MD_FLMTIME | OBD_MD_FLCTIME |
712                                OBD_MD_FLGROUP | OBD_MD_FLEPOCH;
713         oinfo.oi_capa = capa;
714         if (sync) {
715                 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
716                 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
717         }
718
719         set = ptlrpc_prep_set();
720         if (set == NULL) {
721                 CERROR("can't allocate ptlrpc set\n");
722                 rc = -ENOMEM;
723         } else {
724                 rc = obd_getattr_async(exp, &oinfo, set);
725                 if (rc == 0)
726                         rc = ptlrpc_set_wait(set);
727                 ptlrpc_set_destroy(set);
728         }
729         if (rc == 0)
730                 oinfo.oi_oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
731                                          OBD_MD_FLATIME | OBD_MD_FLMTIME |
732                                          OBD_MD_FLCTIME | OBD_MD_FLSIZE);
733         RETURN(rc);
734 }
735
736 /**
737   * Performs the getattr on the inode and updates its fields.
738   * If @sync != 0, perform the getattr under the server-side lock.
739   */
740 int ll_inode_getattr(struct inode *inode, struct obdo *obdo,
741                      __u64 ioepoch, int sync)
742 {
743         struct ll_inode_info *lli  = ll_i2info(inode);
744         struct obd_capa      *capa = ll_mdscapa_get(inode);
745         int rc;
746         ENTRY;
747
748         rc = ll_lsm_getattr(lli->lli_smd, ll_i2dtexp(inode),
749                             capa, obdo, ioepoch, sync);
750         capa_put(capa);
751         if (rc == 0) {
752                 obdo_refresh_inode(inode, obdo, obdo->o_valid);
753                 CDEBUG(D_INODE,
754                        "objid "LPX64" size %Lu, blocks %llu, blksize %lu\n",
755                        lli->lli_smd->lsm_object_id, i_size_read(inode),
756                        (unsigned long long)inode->i_blocks,
757                        (unsigned long)ll_inode_blksize(inode));
758         }
759         RETURN(rc);
760 }
761
762 int ll_merge_lvb(struct inode *inode)
763 {
764         struct ll_inode_info *lli = ll_i2info(inode);
765         struct ll_sb_info *sbi = ll_i2sbi(inode);
766         struct ost_lvb lvb;
767         int rc;
768
769         ENTRY;
770
771         ll_inode_size_lock(inode, 1);
772         inode_init_lvb(inode, &lvb);
773
774         /* merge timestamps the most resently obtained from mds with
775            timestamps obtained from osts */
776         lvb.lvb_atime = lli->lli_lvb.lvb_atime;
777         lvb.lvb_mtime = lli->lli_lvb.lvb_mtime;
778         lvb.lvb_ctime = lli->lli_lvb.lvb_ctime;
779         rc = obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
780         cl_isize_write_nolock(inode, lvb.lvb_size);
781
782         CDEBUG(D_VFSTRACE, DFID" updating i_size "LPU64"\n",
783                PFID(&lli->lli_fid), lvb.lvb_size);
784         inode->i_blocks = lvb.lvb_blocks;
785
786         LTIME_S(inode->i_mtime) = lvb.lvb_mtime;
787         LTIME_S(inode->i_atime) = lvb.lvb_atime;
788         LTIME_S(inode->i_ctime) = lvb.lvb_ctime;
789         ll_inode_size_unlock(inode, 1);
790
791         RETURN(rc);
792 }
793
794 int ll_glimpse_ioctl(struct ll_sb_info *sbi, struct lov_stripe_md *lsm,
795                      lstat_t *st)
796 {
797         struct obdo obdo = { 0 };
798         int rc;
799
800         rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, NULL, &obdo, 0, 0);
801         if (rc == 0) {
802                 st->st_size   = obdo.o_size;
803                 st->st_blocks = obdo.o_blocks;
804                 st->st_mtime  = obdo.o_mtime;
805                 st->st_atime  = obdo.o_atime;
806                 st->st_ctime  = obdo.o_ctime;
807         }
808         return rc;
809 }
810
811 void ll_io_init(struct cl_io *io, const struct file *file, int write)
812 {
813         struct inode *inode = file->f_dentry->d_inode;
814
815         memset(io, 0, sizeof *io);
816         io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
817         if (write)
818                 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
819         io->ci_obj     = ll_i2info(inode)->lli_clob;
820         io->ci_lockreq = CILR_MAYBE;
821         if (ll_file_nolock(file)) {
822                 io->ci_lockreq = CILR_NEVER;
823                 io->ci_no_srvlock = 1;
824         } else if (file->f_flags & O_APPEND) {
825                 io->ci_lockreq = CILR_MANDATORY;
826         }
827 }
828
829 static ssize_t ll_file_io_generic(const struct lu_env *env,
830                 struct vvp_io_args *args, struct file *file,
831                 enum cl_io_type iot, loff_t *ppos, size_t count)
832 {
833         struct cl_io       *io;
834         ssize_t             result;
835         ENTRY;
836
837         io = &ccc_env_info(env)->cti_io;
838         ll_io_init(io, file, iot == CIT_WRITE);
839
840         if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
841                 struct vvp_io *vio = vvp_env_io(env);
842                 struct ccc_io *cio = ccc_env_io(env);
843                 struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
844                 int write_sem_locked = 0;
845
846                 cio->cui_fd  = LUSTRE_FPRIVATE(file);
847                 vio->cui_io_subtype = args->via_io_subtype;
848
849                 switch (vio->cui_io_subtype) {
850                 case IO_NORMAL:
851                         cio->cui_iov = args->u.normal.via_iov;
852                         cio->cui_nrsegs = args->u.normal.via_nrsegs;
853                         cio->cui_tot_nrsegs = cio->cui_nrsegs;
854 #ifndef HAVE_FILE_WRITEV
855                         cio->cui_iocb = args->u.normal.via_iocb;
856 #endif
857                         if ((iot == CIT_WRITE) &&
858                             !(cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
859                                 if(cfs_down_interruptible(&lli->lli_write_sem))
860                                         GOTO(out, result = -ERESTARTSYS);
861                                 write_sem_locked = 1;
862                         } else if (iot == CIT_READ) {
863                                 cfs_down_read(&lli->lli_trunc_sem);
864                         }
865                         break;
866                 case IO_SENDFILE:
867                         vio->u.sendfile.cui_actor = args->u.sendfile.via_actor;
868                         vio->u.sendfile.cui_target = args->u.sendfile.via_target;
869                         break;
870                 case IO_SPLICE:
871                         vio->u.splice.cui_pipe = args->u.splice.via_pipe;
872                         vio->u.splice.cui_flags = args->u.splice.via_flags;
873                         break;
874                 default:
875                         CERROR("Unknow IO type - %u\n", vio->cui_io_subtype);
876                         LBUG();
877                 }
878                 result = cl_io_loop(env, io);
879                 if (write_sem_locked)
880                         cfs_up(&lli->lli_write_sem);
881                 else if (args->via_io_subtype == IO_NORMAL && iot == CIT_READ)
882                         cfs_up_read(&lli->lli_trunc_sem);
883         } else {
884                 /* cl_io_rw_init() handled IO */
885                 result = io->ci_result;
886         }
887
888         if (io->ci_nob > 0) {
889                 result = io->ci_nob;
890                 *ppos = io->u.ci_wr.wr.crw_pos;
891         }
892         GOTO(out, result);
893 out:
894         cl_io_fini(env, io);
895         return result;
896 }
897
898
899 /*
900  * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
901  */
902 static int ll_file_get_iov_count(const struct iovec *iov,
903                                  unsigned long *nr_segs, size_t *count)
904 {
905         size_t cnt = 0;
906         unsigned long seg;
907
908         for (seg = 0; seg < *nr_segs; seg++) {
909                 const struct iovec *iv = &iov[seg];
910
911                 /*
912                  * If any segment has a negative length, or the cumulative
913                  * length ever wraps negative then return -EINVAL.
914                  */
915                 cnt += iv->iov_len;
916                 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
917                         return -EINVAL;
918                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
919                         continue;
920                 if (seg == 0)
921                         return -EFAULT;
922                 *nr_segs = seg;
923                 cnt -= iv->iov_len;   /* This segment is no good */
924                 break;
925         }
926         *count = cnt;
927         return 0;
928 }
929
930 #ifdef HAVE_FILE_READV
931 static ssize_t ll_file_readv(struct file *file, const struct iovec *iov,
932                               unsigned long nr_segs, loff_t *ppos)
933 {
934         struct lu_env      *env;
935         struct vvp_io_args *args;
936         size_t              count;
937         ssize_t             result;
938         int                 refcheck;
939         ENTRY;
940
941         result = ll_file_get_iov_count(iov, &nr_segs, &count);
942         if (result)
943                 RETURN(result);
944
945         env = cl_env_get(&refcheck);
946         if (IS_ERR(env))
947                 RETURN(PTR_ERR(env));
948
949         args = vvp_env_args(env, IO_NORMAL);
950         args->u.normal.via_iov = (struct iovec *)iov;
951         args->u.normal.via_nrsegs = nr_segs;
952
953         result = ll_file_io_generic(env, args, file, CIT_READ, ppos, count);
954         cl_env_put(env, &refcheck);
955         RETURN(result);
956 }
957
958 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
959                             loff_t *ppos)
960 {
961         struct lu_env *env;
962         struct iovec  *local_iov;
963         ssize_t        result;
964         int            refcheck;
965         ENTRY;
966
967         env = cl_env_get(&refcheck);
968         if (IS_ERR(env))
969                 RETURN(PTR_ERR(env));
970
971         local_iov = &vvp_env_info(env)->vti_local_iov;
972         local_iov->iov_base = (void __user *)buf;
973         local_iov->iov_len = count;
974         result = ll_file_readv(file, local_iov, 1, ppos);
975         cl_env_put(env, &refcheck);
976         RETURN(result);
977 }
978
979 #else
980 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
981                                 unsigned long nr_segs, loff_t pos)
982 {
983         struct lu_env      *env;
984         struct vvp_io_args *args;
985         size_t              count;
986         ssize_t             result;
987         int                 refcheck;
988         ENTRY;
989
990         result = ll_file_get_iov_count(iov, &nr_segs, &count);
991         if (result)
992                 RETURN(result);
993
994         env = cl_env_get(&refcheck);
995         if (IS_ERR(env))
996                 RETURN(PTR_ERR(env));
997
998         args = vvp_env_args(env, IO_NORMAL);
999         args->u.normal.via_iov = (struct iovec *)iov;
1000         args->u.normal.via_nrsegs = nr_segs;
1001         args->u.normal.via_iocb = iocb;
1002
1003         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1004                                     &iocb->ki_pos, count);
1005         cl_env_put(env, &refcheck);
1006         RETURN(result);
1007 }
1008
1009 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
1010                             loff_t *ppos)
1011 {
1012         struct lu_env *env;
1013         struct iovec  *local_iov;
1014         struct kiocb  *kiocb;
1015         ssize_t        result;
1016         int            refcheck;
1017         ENTRY;
1018
1019         env = cl_env_get(&refcheck);
1020         if (IS_ERR(env))
1021                 RETURN(PTR_ERR(env));
1022
1023         local_iov = &vvp_env_info(env)->vti_local_iov;
1024         kiocb = &vvp_env_info(env)->vti_kiocb;
1025         local_iov->iov_base = (void __user *)buf;
1026         local_iov->iov_len = count;
1027         init_sync_kiocb(kiocb, file);
1028         kiocb->ki_pos = *ppos;
1029         kiocb->ki_left = count;
1030
1031         result = ll_file_aio_read(kiocb, local_iov, 1, kiocb->ki_pos);
1032         *ppos = kiocb->ki_pos;
1033
1034         cl_env_put(env, &refcheck);
1035         RETURN(result);
1036 }
1037 #endif
1038
1039 /*
1040  * Write to a file (through the page cache).
1041  */
1042 #ifdef HAVE_FILE_WRITEV
1043 static ssize_t ll_file_writev(struct file *file, const struct iovec *iov,
1044                               unsigned long nr_segs, loff_t *ppos)
1045 {
1046         struct lu_env      *env;
1047         struct vvp_io_args *args;
1048         size_t              count;
1049         ssize_t             result;
1050         int                 refcheck;
1051         ENTRY;
1052
1053         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1054         if (result)
1055                 RETURN(result);
1056
1057         env = cl_env_get(&refcheck);
1058         if (IS_ERR(env))
1059                 RETURN(PTR_ERR(env));
1060
1061         args = vvp_env_args(env, IO_NORMAL);
1062         args->u.normal.via_iov = (struct iovec *)iov;
1063         args->u.normal.via_nrsegs = nr_segs;
1064
1065         result = ll_file_io_generic(env, args, file, CIT_WRITE, ppos, count);
1066         cl_env_put(env, &refcheck);
1067         RETURN(result);
1068 }
1069
1070 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1071                              loff_t *ppos)
1072 {
1073         struct lu_env    *env;
1074         struct iovec     *local_iov;
1075         ssize_t           result;
1076         int               refcheck;
1077         ENTRY;
1078
1079         env = cl_env_get(&refcheck);
1080         if (IS_ERR(env))
1081                 RETURN(PTR_ERR(env));
1082
1083         local_iov = &vvp_env_info(env)->vti_local_iov;
1084         local_iov->iov_base = (void __user *)buf;
1085         local_iov->iov_len = count;
1086
1087         result = ll_file_writev(file, local_iov, 1, ppos);
1088         cl_env_put(env, &refcheck);
1089         RETURN(result);
1090 }
1091
1092 #else /* AIO stuff */
1093 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1094                                  unsigned long nr_segs, loff_t pos)
1095 {
1096         struct lu_env      *env;
1097         struct vvp_io_args *args;
1098         size_t              count;
1099         ssize_t             result;
1100         int                 refcheck;
1101         ENTRY;
1102
1103         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1104         if (result)
1105                 RETURN(result);
1106
1107         env = cl_env_get(&refcheck);
1108         if (IS_ERR(env))
1109                 RETURN(PTR_ERR(env));
1110
1111         args = vvp_env_args(env, IO_NORMAL);
1112         args->u.normal.via_iov = (struct iovec *)iov;
1113         args->u.normal.via_nrsegs = nr_segs;
1114         args->u.normal.via_iocb = iocb;
1115
1116         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1117                                   &iocb->ki_pos, count);
1118         cl_env_put(env, &refcheck);
1119         RETURN(result);
1120 }
1121
1122 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1123                              loff_t *ppos)
1124 {
1125         struct lu_env *env;
1126         struct iovec  *local_iov;
1127         struct kiocb  *kiocb;
1128         ssize_t        result;
1129         int            refcheck;
1130         ENTRY;
1131
1132         env = cl_env_get(&refcheck);
1133         if (IS_ERR(env))
1134                 RETURN(PTR_ERR(env));
1135
1136         local_iov = &vvp_env_info(env)->vti_local_iov;
1137         kiocb = &vvp_env_info(env)->vti_kiocb;
1138         local_iov->iov_base = (void __user *)buf;
1139         local_iov->iov_len = count;
1140         init_sync_kiocb(kiocb, file);
1141         kiocb->ki_pos = *ppos;
1142         kiocb->ki_left = count;
1143
1144         result = ll_file_aio_write(kiocb, local_iov, 1, kiocb->ki_pos);
1145         *ppos = kiocb->ki_pos;
1146
1147         cl_env_put(env, &refcheck);
1148         RETURN(result);
1149 }
1150 #endif
1151
1152
1153 #ifdef HAVE_KERNEL_SENDFILE
1154 /*
1155  * Send file content (through pagecache) somewhere with helper
1156  */
1157 static ssize_t ll_file_sendfile(struct file *in_file, loff_t *ppos,size_t count,
1158                                 read_actor_t actor, void *target)
1159 {
1160         struct lu_env      *env;
1161         struct vvp_io_args *args;
1162         ssize_t             result;
1163         int                 refcheck;
1164         ENTRY;
1165
1166         env = cl_env_get(&refcheck);
1167         if (IS_ERR(env))
1168                 RETURN(PTR_ERR(env));
1169
1170         args = vvp_env_args(env, IO_SENDFILE);
1171         args->u.sendfile.via_target = target;
1172         args->u.sendfile.via_actor = actor;
1173
1174         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1175         cl_env_put(env, &refcheck);
1176         RETURN(result);
1177 }
1178 #endif
1179
1180 #ifdef HAVE_KERNEL_SPLICE_READ
1181 /*
1182  * Send file content (through pagecache) somewhere with helper
1183  */
1184 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1185                                    struct pipe_inode_info *pipe, size_t count,
1186                                    unsigned int flags)
1187 {
1188         struct lu_env      *env;
1189         struct vvp_io_args *args;
1190         ssize_t             result;
1191         int                 refcheck;
1192         ENTRY;
1193
1194         env = cl_env_get(&refcheck);
1195         if (IS_ERR(env))
1196                 RETURN(PTR_ERR(env));
1197
1198         args = vvp_env_args(env, IO_SPLICE);
1199         args->u.splice.via_pipe = pipe;
1200         args->u.splice.via_flags = flags;
1201
1202         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1203         cl_env_put(env, &refcheck);
1204         RETURN(result);
1205 }
1206 #endif
1207
1208 static int ll_lov_recreate(struct inode *inode, obd_id id, obd_seq seq,
1209                            obd_count ost_idx)
1210 {
1211         struct obd_export *exp = ll_i2dtexp(inode);
1212         struct obd_trans_info oti = { 0 };
1213         struct obdo *oa = NULL;
1214         int lsm_size;
1215         int rc = 0;
1216         struct lov_stripe_md *lsm, *lsm2;
1217         ENTRY;
1218
1219         OBDO_ALLOC(oa);
1220         if (oa == NULL)
1221                 RETURN(-ENOMEM);
1222
1223         ll_inode_size_lock(inode, 0);
1224         lsm = ll_i2info(inode)->lli_smd;
1225         if (lsm == NULL)
1226                 GOTO(out, rc = -ENOENT);
1227         lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
1228                    (lsm->lsm_stripe_count));
1229
1230         OBD_ALLOC(lsm2, lsm_size);
1231         if (lsm2 == NULL)
1232                 GOTO(out, rc = -ENOMEM);
1233
1234         oa->o_id = id;
1235         oa->o_seq = seq;
1236         oa->o_nlink = ost_idx;
1237         oa->o_flags |= OBD_FL_RECREATE_OBJS;
1238         oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1239         obdo_from_inode(oa, inode, &ll_i2info(inode)->lli_fid, OBD_MD_FLTYPE |
1240                         OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1241         memcpy(lsm2, lsm, lsm_size);
1242         rc = obd_create(exp, oa, &lsm2, &oti);
1243
1244         OBD_FREE(lsm2, lsm_size);
1245         GOTO(out, rc);
1246 out:
1247         ll_inode_size_unlock(inode, 0);
1248         OBDO_FREE(oa);
1249         return rc;
1250 }
1251
1252 static int ll_lov_recreate_obj(struct inode *inode, unsigned long arg)
1253 {
1254         struct ll_recreate_obj ucreat;
1255         ENTRY;
1256
1257         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1258                 RETURN(-EPERM);
1259
1260         if (cfs_copy_from_user(&ucreat, (struct ll_recreate_obj *)arg,
1261                                sizeof(struct ll_recreate_obj)))
1262                 RETURN(-EFAULT);
1263
1264         RETURN(ll_lov_recreate(inode, ucreat.lrc_id, 0,
1265                                ucreat.lrc_ost_idx));
1266 }
1267
1268 static int ll_lov_recreate_fid(struct inode *inode, unsigned long arg)
1269 {
1270         struct lu_fid fid;
1271         obd_id id;
1272         obd_count ost_idx;
1273         ENTRY;
1274
1275         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1276                 RETURN(-EPERM);
1277
1278         if (cfs_copy_from_user(&fid, (struct lu_fid *)arg,
1279                                sizeof(struct lu_fid)))
1280                 RETURN(-EFAULT);
1281
1282         id = fid_oid(&fid) | ((fid_seq(&fid) & 0xffff) << 32);
1283         ost_idx = (fid_seq(&fid) >> 16) & 0xffff;
1284         RETURN(ll_lov_recreate(inode, id, 0, ost_idx));
1285 }
1286
1287 int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1288                              int flags, struct lov_user_md *lum, int lum_size)
1289 {
1290         struct lov_stripe_md *lsm;
1291         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1292         int rc = 0;
1293         ENTRY;
1294
1295         ll_inode_size_lock(inode, 0);
1296         lsm = ll_i2info(inode)->lli_smd;
1297         if (lsm) {
1298                 ll_inode_size_unlock(inode, 0);
1299                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1300                        inode->i_ino);
1301                 RETURN(-EEXIST);
1302         }
1303
1304         rc = ll_intent_file_open(file, lum, lum_size, &oit);
1305         if (rc)
1306                 GOTO(out, rc);
1307         if (it_disposition(&oit, DISP_LOOKUP_NEG))
1308                 GOTO(out_req_free, rc = -ENOENT);
1309         rc = oit.d.lustre.it_status;
1310         if (rc < 0)
1311                 GOTO(out_req_free, rc);
1312
1313         ll_release_openhandle(file->f_dentry, &oit);
1314
1315  out:
1316         ll_inode_size_unlock(inode, 0);
1317         ll_intent_release(&oit);
1318         RETURN(rc);
1319 out_req_free:
1320         ptlrpc_req_finished((struct ptlrpc_request *) oit.d.lustre.it_data);
1321         goto out;
1322 }
1323
1324 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1325                              struct lov_mds_md **lmmp, int *lmm_size,
1326                              struct ptlrpc_request **request)
1327 {
1328         struct ll_sb_info *sbi = ll_i2sbi(inode);
1329         struct mdt_body  *body;
1330         struct lov_mds_md *lmm = NULL;
1331         struct ptlrpc_request *req = NULL;
1332         struct md_op_data *op_data;
1333         int rc, lmmsize;
1334
1335         rc = ll_get_max_mdsize(sbi, &lmmsize);
1336         if (rc)
1337                 RETURN(rc);
1338
1339         op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1340                                      strlen(filename), lmmsize,
1341                                      LUSTRE_OPC_ANY, NULL);
1342         if (op_data == NULL)
1343                 RETURN(-ENOMEM);
1344
1345         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1346         rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1347         ll_finish_md_op_data(op_data);
1348         if (rc < 0) {
1349                 CDEBUG(D_INFO, "md_getattr_name failed "
1350                        "on %s: rc %d\n", filename, rc);
1351                 GOTO(out, rc);
1352         }
1353
1354         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1355         LASSERT(body != NULL); /* checked by mdc_getattr_name */
1356
1357         lmmsize = body->eadatasize;
1358
1359         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1360                         lmmsize == 0) {
1361                 GOTO(out, rc = -ENODATA);
1362         }
1363
1364         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1365         LASSERT(lmm != NULL);
1366
1367         if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) &&
1368             (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) {
1369                 GOTO(out, rc = -EPROTO);
1370         }
1371
1372         /*
1373          * This is coming from the MDS, so is probably in
1374          * little endian.  We convert it to host endian before
1375          * passing it to userspace.
1376          */
1377         if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
1378                 /* if function called for directory - we should
1379                  * avoid swab not existent lsm objects */
1380                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1381                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1382                         if (S_ISREG(body->mode))
1383                                 lustre_swab_lov_user_md_objects(
1384                                  ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1385                                  ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1386                 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1387                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1388                         if (S_ISREG(body->mode))
1389                                 lustre_swab_lov_user_md_objects(
1390                                  ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1391                                  ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1392                 }
1393         }
1394
1395 out:
1396         *lmmp = lmm;
1397         *lmm_size = lmmsize;
1398         *request = req;
1399         return rc;
1400 }
1401
1402 static int ll_lov_setea(struct inode *inode, struct file *file,
1403                             unsigned long arg)
1404 {
1405         int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1406         struct lov_user_md  *lump;
1407         int lum_size = sizeof(struct lov_user_md) +
1408                        sizeof(struct lov_user_ost_data);
1409         int rc;
1410         ENTRY;
1411
1412         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1413                 RETURN(-EPERM);
1414
1415         OBD_ALLOC(lump, lum_size);
1416         if (lump == NULL) {
1417                 RETURN(-ENOMEM);
1418         }
1419         if (cfs_copy_from_user(lump, (struct lov_user_md  *)arg, lum_size)) {
1420                 OBD_FREE(lump, lum_size);
1421                 RETURN(-EFAULT);
1422         }
1423
1424         rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1425
1426         OBD_FREE(lump, lum_size);
1427         RETURN(rc);
1428 }
1429
1430 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1431                             unsigned long arg)
1432 {
1433         struct lov_user_md_v3 lumv3;
1434         struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1435         struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1436         struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1437         int lum_size;
1438         int rc;
1439         int flags = FMODE_WRITE;
1440         ENTRY;
1441
1442         /* first try with v1 which is smaller than v3 */
1443         lum_size = sizeof(struct lov_user_md_v1);
1444         if (cfs_copy_from_user(lumv1, lumv1p, lum_size))
1445                 RETURN(-EFAULT);
1446
1447         if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1448                 lum_size = sizeof(struct lov_user_md_v3);
1449                 if (cfs_copy_from_user(&lumv3, lumv3p, lum_size))
1450                         RETURN(-EFAULT);
1451         }
1452
1453         rc = ll_lov_setstripe_ea_info(inode, file, flags, lumv1, lum_size);
1454         if (rc == 0) {
1455                  put_user(0, &lumv1p->lmm_stripe_count);
1456                  rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1457                                     0, ll_i2info(inode)->lli_smd,
1458                                     (void *)arg);
1459         }
1460         RETURN(rc);
1461 }
1462
1463 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1464 {
1465         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1466
1467         if (!lsm)
1468                 RETURN(-ENODATA);
1469
1470         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0, lsm,
1471                             (void *)arg);
1472 }
1473
1474 int ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1475 {
1476         struct ll_inode_info   *lli = ll_i2info(inode);
1477         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1478         struct ccc_grouplock    grouplock;
1479         int                     rc;
1480         ENTRY;
1481
1482         if (ll_file_nolock(file))
1483                 RETURN(-EOPNOTSUPP);
1484
1485         cfs_spin_lock(&lli->lli_lock);
1486         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1487                 CWARN("group lock already existed with gid %lu\n",
1488                        fd->fd_grouplock.cg_gid);
1489                 cfs_spin_unlock(&lli->lli_lock);
1490                 RETURN(-EINVAL);
1491         }
1492         LASSERT(fd->fd_grouplock.cg_lock == NULL);
1493         cfs_spin_unlock(&lli->lli_lock);
1494
1495         rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1496                               arg, (file->f_flags & O_NONBLOCK), &grouplock);
1497         if (rc)
1498                 RETURN(rc);
1499
1500         cfs_spin_lock(&lli->lli_lock);
1501         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1502                 cfs_spin_unlock(&lli->lli_lock);
1503                 CERROR("another thread just won the race\n");
1504                 cl_put_grouplock(&grouplock);
1505                 RETURN(-EINVAL);
1506         }
1507
1508         fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1509         fd->fd_grouplock = grouplock;
1510         cfs_spin_unlock(&lli->lli_lock);
1511
1512         CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
1513         RETURN(0);
1514 }
1515
1516 int ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1517 {
1518         struct ll_inode_info   *lli = ll_i2info(inode);
1519         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1520         struct ccc_grouplock    grouplock;
1521         ENTRY;
1522
1523         cfs_spin_lock(&lli->lli_lock);
1524         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1525                 cfs_spin_unlock(&lli->lli_lock);
1526                 CWARN("no group lock held\n");
1527                 RETURN(-EINVAL);
1528         }
1529         LASSERT(fd->fd_grouplock.cg_lock != NULL);
1530
1531         if (fd->fd_grouplock.cg_gid != arg) {
1532                 CWARN("group lock %lu doesn't match current id %lu\n",
1533                        arg, fd->fd_grouplock.cg_gid);
1534                 cfs_spin_unlock(&lli->lli_lock);
1535                 RETURN(-EINVAL);
1536         }
1537
1538         grouplock = fd->fd_grouplock;
1539         memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1540         fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1541         cfs_spin_unlock(&lli->lli_lock);
1542
1543         cl_put_grouplock(&grouplock);
1544         CDEBUG(D_INFO, "group lock %lu released\n", arg);
1545         RETURN(0);
1546 }
1547
1548 /**
1549  * Close inode open handle
1550  *
1551  * \param dentry [in]     dentry which contains the inode
1552  * \param it     [in,out] intent which contains open info and result
1553  *
1554  * \retval 0     success
1555  * \retval <0    failure
1556  */
1557 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
1558 {
1559         struct inode *inode = dentry->d_inode;
1560         struct obd_client_handle *och;
1561         int rc;
1562         ENTRY;
1563
1564         LASSERT(inode);
1565
1566         /* Root ? Do nothing. */
1567         if (dentry->d_inode->i_sb->s_root == dentry)
1568                 RETURN(0);
1569
1570         /* No open handle to close? Move away */
1571         if (!it_disposition(it, DISP_OPEN_OPEN))
1572                 RETURN(0);
1573
1574         LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
1575
1576         OBD_ALLOC(och, sizeof(*och));
1577         if (!och)
1578                 GOTO(out, rc = -ENOMEM);
1579
1580         ll_och_fill(ll_i2sbi(inode)->ll_md_exp,
1581                     ll_i2info(inode), it, och);
1582
1583         rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
1584                                        inode, och);
1585  out:
1586         /* this one is in place of ll_file_open */
1587         if (it_disposition(it, DISP_ENQ_OPEN_REF))
1588                 ptlrpc_req_finished(it->d.lustre.it_data);
1589         it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1590         RETURN(rc);
1591 }
1592
1593 /**
1594  * Get size for inode for which FIEMAP mapping is requested.
1595  * Make the FIEMAP get_info call and returns the result.
1596  */
1597 int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
1598               int num_bytes)
1599 {
1600         struct obd_export *exp = ll_i2dtexp(inode);
1601         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1602         struct ll_fiemap_info_key fm_key = { .name = KEY_FIEMAP, };
1603         int vallen = num_bytes;
1604         int rc;
1605         ENTRY;
1606
1607         /* Checks for fiemap flags */
1608         if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
1609                 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
1610                 return -EBADR;
1611         }
1612
1613         /* Check for FIEMAP_FLAG_SYNC */
1614         if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
1615                 rc = filemap_fdatawrite(inode->i_mapping);
1616                 if (rc)
1617                         return rc;
1618         }
1619
1620         /* If the stripe_count > 1 and the application does not understand
1621          * DEVICE_ORDER flag, then it cannot interpret the extents correctly.
1622          */
1623         if (lsm->lsm_stripe_count > 1 &&
1624             !(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER))
1625                 return -EOPNOTSUPP;
1626
1627         fm_key.oa.o_id = lsm->lsm_object_id;
1628         fm_key.oa.o_seq = lsm->lsm_object_seq;
1629         fm_key.oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1630
1631         obdo_from_inode(&fm_key.oa, inode, &ll_i2info(inode)->lli_fid,
1632                         OBD_MD_FLSIZE);
1633         /* If filesize is 0, then there would be no objects for mapping */
1634         if (fm_key.oa.o_size == 0) {
1635                 fiemap->fm_mapped_extents = 0;
1636                 RETURN(0);
1637         }
1638
1639         memcpy(&fm_key.fiemap, fiemap, sizeof(*fiemap));
1640
1641         rc = obd_get_info(exp, sizeof(fm_key), &fm_key, &vallen, fiemap, lsm);
1642         if (rc)
1643                 CERROR("obd_get_info failed: rc = %d\n", rc);
1644
1645         RETURN(rc);
1646 }
1647
1648 int ll_fid2path(struct obd_export *exp, void *arg)
1649 {
1650         struct getinfo_fid2path *gfout, *gfin;
1651         int outsize, rc;
1652         ENTRY;
1653
1654         /* Need to get the buflen */
1655         OBD_ALLOC_PTR(gfin);
1656         if (gfin == NULL)
1657                 RETURN(-ENOMEM);
1658         if (cfs_copy_from_user(gfin, arg, sizeof(*gfin))) {
1659                 OBD_FREE_PTR(gfin);
1660                 RETURN(-EFAULT);
1661         }
1662
1663         outsize = sizeof(*gfout) + gfin->gf_pathlen;
1664         OBD_ALLOC(gfout, outsize);
1665         if (gfout == NULL) {
1666                 OBD_FREE_PTR(gfin);
1667                 RETURN(-ENOMEM);
1668         }
1669         memcpy(gfout, gfin, sizeof(*gfout));
1670         OBD_FREE_PTR(gfin);
1671
1672         /* Call mdc_iocontrol */
1673         rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
1674         if (rc)
1675                 GOTO(gf_free, rc);
1676         if (cfs_copy_to_user(arg, gfout, outsize))
1677                 rc = -EFAULT;
1678
1679 gf_free:
1680         OBD_FREE(gfout, outsize);
1681         RETURN(rc);
1682 }
1683
1684 static int ll_ioctl_fiemap(struct inode *inode, unsigned long arg)
1685 {
1686         struct ll_user_fiemap *fiemap_s;
1687         size_t num_bytes, ret_bytes;
1688         unsigned int extent_count;
1689         int rc = 0;
1690
1691         /* Get the extent count so we can calculate the size of
1692          * required fiemap buffer */
1693         if (get_user(extent_count,
1694             &((struct ll_user_fiemap __user *)arg)->fm_extent_count))
1695                 RETURN(-EFAULT);
1696         num_bytes = sizeof(*fiemap_s) + (extent_count *
1697                                          sizeof(struct ll_fiemap_extent));
1698
1699         OBD_VMALLOC(fiemap_s, num_bytes);
1700         if (fiemap_s == NULL)
1701                 RETURN(-ENOMEM);
1702
1703         /* get the fiemap value */
1704         if (copy_from_user(fiemap_s,(struct ll_user_fiemap __user *)arg,
1705                            sizeof(*fiemap_s)))
1706                 GOTO(error, rc = -EFAULT);
1707
1708         /* If fm_extent_count is non-zero, read the first extent since
1709          * it is used to calculate end_offset and device from previous
1710          * fiemap call. */
1711         if (extent_count) {
1712                 if (copy_from_user(&fiemap_s->fm_extents[0],
1713                     (char __user *)arg + sizeof(*fiemap_s),
1714                     sizeof(struct ll_fiemap_extent)))
1715                         GOTO(error, rc = -EFAULT);
1716         }
1717
1718         rc = ll_do_fiemap(inode, fiemap_s, num_bytes);
1719         if (rc)
1720                 GOTO(error, rc);
1721
1722         ret_bytes = sizeof(struct ll_user_fiemap);
1723
1724         if (extent_count != 0)
1725                 ret_bytes += (fiemap_s->fm_mapped_extents *
1726                                  sizeof(struct ll_fiemap_extent));
1727
1728         if (copy_to_user((void *)arg, fiemap_s, ret_bytes))
1729                 rc = -EFAULT;
1730
1731 error:
1732         OBD_VFREE(fiemap_s, num_bytes);
1733         RETURN(rc);
1734 }
1735
1736 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1737                   unsigned long arg)
1738 {
1739         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1740         int flags;
1741         ENTRY;
1742
1743         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1744                inode->i_generation, inode, cmd);
1745         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1746
1747         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1748         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1749                 RETURN(-ENOTTY);
1750
1751         switch(cmd) {
1752         case LL_IOC_GETFLAGS:
1753                 /* Get the current value of the file flags */
1754                 return put_user(fd->fd_flags, (int *)arg);
1755         case LL_IOC_SETFLAGS:
1756         case LL_IOC_CLRFLAGS:
1757                 /* Set or clear specific file flags */
1758                 /* XXX This probably needs checks to ensure the flags are
1759                  *     not abused, and to handle any flag side effects.
1760                  */
1761                 if (get_user(flags, (int *) arg))
1762                         RETURN(-EFAULT);
1763
1764                 if (cmd == LL_IOC_SETFLAGS) {
1765                         if ((flags & LL_FILE_IGNORE_LOCK) &&
1766                             !(file->f_flags & O_DIRECT)) {
1767                                 CERROR("%s: unable to disable locking on "
1768                                        "non-O_DIRECT file\n", current->comm);
1769                                 RETURN(-EINVAL);
1770                         }
1771
1772                         fd->fd_flags |= flags;
1773                 } else {
1774                         fd->fd_flags &= ~flags;
1775                 }
1776                 RETURN(0);
1777         case LL_IOC_LOV_SETSTRIPE:
1778                 RETURN(ll_lov_setstripe(inode, file, arg));
1779         case LL_IOC_LOV_SETEA:
1780                 RETURN(ll_lov_setea(inode, file, arg));
1781         case LL_IOC_LOV_GETSTRIPE:
1782                 RETURN(ll_lov_getstripe(inode, arg));
1783         case LL_IOC_RECREATE_OBJ:
1784                 RETURN(ll_lov_recreate_obj(inode, arg));
1785         case LL_IOC_RECREATE_FID:
1786                 RETURN(ll_lov_recreate_fid(inode, arg));
1787         case FSFILT_IOC_FIEMAP:
1788                 RETURN(ll_ioctl_fiemap(inode, arg));
1789         case FSFILT_IOC_GETFLAGS:
1790         case FSFILT_IOC_SETFLAGS:
1791                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1792         case FSFILT_IOC_GETVERSION_OLD:
1793         case FSFILT_IOC_GETVERSION:
1794                 RETURN(put_user(inode->i_generation, (int *)arg));
1795         case LL_IOC_GROUP_LOCK:
1796                 RETURN(ll_get_grouplock(inode, file, arg));
1797         case LL_IOC_GROUP_UNLOCK:
1798                 RETURN(ll_put_grouplock(inode, file, arg));
1799         case IOC_OBD_STATFS:
1800                 RETURN(ll_obd_statfs(inode, (void *)arg));
1801
1802         /* We need to special case any other ioctls we want to handle,
1803          * to send them to the MDS/OST as appropriate and to properly
1804          * network encode the arg field.
1805         case FSFILT_IOC_SETVERSION_OLD:
1806         case FSFILT_IOC_SETVERSION:
1807         */
1808         case LL_IOC_FLUSHCTX:
1809                 RETURN(ll_flush_ctx(inode));
1810         case LL_IOC_PATH2FID: {
1811                 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1812                                      sizeof(struct lu_fid)))
1813                         RETURN(-EFAULT);
1814
1815                 RETURN(0);
1816         }
1817         case OBD_IOC_FID2PATH:
1818                 RETURN(ll_fid2path(ll_i2mdexp(inode), (void *)arg));
1819
1820         case LL_IOC_GET_MDTIDX: {
1821                 int mdtidx;
1822
1823                 mdtidx = ll_get_mdt_idx(inode);
1824                 if (mdtidx < 0)
1825                         RETURN(mdtidx);
1826
1827                 if (put_user((int)mdtidx, (int*)arg))
1828                         RETURN(-EFAULT);
1829
1830                 RETURN(0);
1831         }
1832
1833         default: {
1834                 int err;
1835
1836                 if (LLIOC_STOP ==
1837                     ll_iocontrol_call(inode, file, cmd, arg, &err))
1838                         RETURN(err);
1839
1840                 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
1841                                      (void *)arg));
1842         }
1843         }
1844 }
1845
1846 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1847 {
1848         struct inode *inode = file->f_dentry->d_inode;
1849         loff_t retval;
1850         ENTRY;
1851         retval = offset + ((origin == 2) ? i_size_read(inode) :
1852                            (origin == 1) ? file->f_pos : 0);
1853         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), to=%Lu=%#Lx(%s)\n",
1854                inode->i_ino, inode->i_generation, inode, retval, retval,
1855                origin == 2 ? "SEEK_END": origin == 1 ? "SEEK_CUR" : "SEEK_SET");
1856         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
1857
1858         if (origin == 2) { /* SEEK_END */
1859                 int nonblock = 0, rc;
1860
1861                 if (file->f_flags & O_NONBLOCK)
1862                         nonblock = LDLM_FL_BLOCK_NOWAIT;
1863
1864                 rc = cl_glimpse_size(inode);
1865                 if (rc != 0)
1866                         RETURN(rc);
1867
1868                 offset += i_size_read(inode);
1869         } else if (origin == 1) { /* SEEK_CUR */
1870                 offset += file->f_pos;
1871         }
1872
1873         retval = -EINVAL;
1874         if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1875                 if (offset != file->f_pos) {
1876                         file->f_pos = offset;
1877                 }
1878                 retval = offset;
1879         }
1880
1881         RETURN(retval);
1882 }
1883
1884 #ifdef HAVE_FLUSH_OWNER_ID
1885 int ll_flush(struct file *file, fl_owner_t id)
1886 #else
1887 int ll_flush(struct file *file)
1888 #endif
1889 {
1890         struct inode *inode = file->f_dentry->d_inode;
1891         struct ll_inode_info *lli = ll_i2info(inode);
1892         struct lov_stripe_md *lsm = lli->lli_smd;
1893         int rc, err;
1894
1895         /* catch async errors that were recorded back when async writeback
1896          * failed for pages in this mapping. */
1897         rc = lli->lli_async_rc;
1898         lli->lli_async_rc = 0;
1899         if (lsm) {
1900                 err = lov_test_and_clear_async_rc(lsm);
1901                 if (rc == 0)
1902                         rc = err;
1903         }
1904
1905         return rc ? -EIO : 0;
1906 }
1907
1908 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1909 {
1910         struct inode *inode = dentry->d_inode;
1911         struct ll_inode_info *lli = ll_i2info(inode);
1912         struct lov_stripe_md *lsm = lli->lli_smd;
1913         struct ptlrpc_request *req;
1914         struct obd_capa *oc;
1915         int rc, err;
1916         ENTRY;
1917         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1918                inode->i_generation, inode);
1919         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
1920
1921         /* fsync's caller has already called _fdata{sync,write}, we want
1922          * that IO to finish before calling the osc and mdc sync methods */
1923         rc = filemap_fdatawait(inode->i_mapping);
1924
1925         /* catch async errors that were recorded back when async writeback
1926          * failed for pages in this mapping. */
1927         err = lli->lli_async_rc;
1928         lli->lli_async_rc = 0;
1929         if (rc == 0)
1930                 rc = err;
1931         if (lsm) {
1932                 err = lov_test_and_clear_async_rc(lsm);
1933                 if (rc == 0)
1934                         rc = err;
1935         }
1936
1937         oc = ll_mdscapa_get(inode);
1938         err = md_sync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), oc,
1939                       &req);
1940         capa_put(oc);
1941         if (!rc)
1942                 rc = err;
1943         if (!err)
1944                 ptlrpc_req_finished(req);
1945
1946         if (data && lsm) {
1947                 struct obdo *oa;
1948
1949                 OBDO_ALLOC(oa);
1950                 if (!oa)
1951                         RETURN(rc ? rc : -ENOMEM);
1952
1953                 oa->o_id = lsm->lsm_object_id;
1954                 oa->o_seq = lsm->lsm_object_seq;
1955                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1956                 obdo_from_inode(oa, inode, &ll_i2info(inode)->lli_fid,
1957                                 OBD_MD_FLTYPE | OBD_MD_FLATIME |
1958                                 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1959                                 OBD_MD_FLGROUP);
1960
1961                 oc = ll_osscapa_get(inode, CAPA_OPC_OSS_WRITE);
1962                 err = obd_sync(ll_i2sbi(inode)->ll_dt_exp, oa, lsm,
1963                                0, OBD_OBJECT_EOF, oc);
1964                 capa_put(oc);
1965                 if (!rc)
1966                         rc = err;
1967                 OBDO_FREE(oa);
1968         }
1969
1970         RETURN(rc);
1971 }
1972
1973 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
1974 {
1975         struct inode *inode = file->f_dentry->d_inode;
1976         struct ll_sb_info *sbi = ll_i2sbi(inode);
1977         struct ldlm_enqueue_info einfo = { .ei_type = LDLM_FLOCK,
1978                                            .ei_cb_cp =ldlm_flock_completion_ast,
1979                                            .ei_cbdata = file_lock };
1980         struct md_op_data *op_data;
1981         struct lustre_handle lockh = {0};
1982         ldlm_policy_data_t flock;
1983         int flags = 0;
1984         int rc;
1985         ENTRY;
1986
1987         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
1988                inode->i_ino, file_lock);
1989
1990         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
1991
1992         if (file_lock->fl_flags & FL_FLOCK) {
1993                 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
1994                 /* set missing params for flock() calls */
1995                 file_lock->fl_end = OFFSET_MAX;
1996                 file_lock->fl_pid = current->tgid;
1997         }
1998         flock.l_flock.pid = file_lock->fl_pid;
1999         flock.l_flock.start = file_lock->fl_start;
2000         flock.l_flock.end = file_lock->fl_end;
2001
2002         switch (file_lock->fl_type) {
2003         case F_RDLCK:
2004                 einfo.ei_mode = LCK_PR;
2005                 break;
2006         case F_UNLCK:
2007                 /* An unlock request may or may not have any relation to
2008                  * existing locks so we may not be able to pass a lock handle
2009                  * via a normal ldlm_lock_cancel() request. The request may even
2010                  * unlock a byte range in the middle of an existing lock. In
2011                  * order to process an unlock request we need all of the same
2012                  * information that is given with a normal read or write record
2013                  * lock request. To avoid creating another ldlm unlock (cancel)
2014                  * message we'll treat a LCK_NL flock request as an unlock. */
2015                 einfo.ei_mode = LCK_NL;
2016                 break;
2017         case F_WRLCK:
2018                 einfo.ei_mode = LCK_PW;
2019                 break;
2020         default:
2021                 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
2022                 RETURN (-EINVAL);
2023         }
2024
2025         switch (cmd) {
2026         case F_SETLKW:
2027 #ifdef F_SETLKW64
2028         case F_SETLKW64:
2029 #endif
2030                 flags = 0;
2031                 break;
2032         case F_SETLK:
2033 #ifdef F_SETLK64
2034         case F_SETLK64:
2035 #endif
2036                 flags = LDLM_FL_BLOCK_NOWAIT;
2037                 break;
2038         case F_GETLK:
2039 #ifdef F_GETLK64
2040         case F_GETLK64:
2041 #endif
2042                 flags = LDLM_FL_TEST_LOCK;
2043                 /* Save the old mode so that if the mode in the lock changes we
2044                  * can decrement the appropriate reader or writer refcount. */
2045                 file_lock->fl_type = einfo.ei_mode;
2046                 break;
2047         default:
2048                 CERROR("unknown fcntl lock command: %d\n", cmd);
2049                 RETURN (-EINVAL);
2050         }
2051
2052         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2053                                      LUSTRE_OPC_ANY, NULL);
2054         if (IS_ERR(op_data))
2055                 RETURN(PTR_ERR(op_data));
2056
2057         CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
2058                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
2059                flags, einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
2060
2061         rc = md_enqueue(sbi->ll_md_exp, &einfo, NULL,
2062                         op_data, &lockh, &flock, 0, NULL /* req */, flags);
2063
2064         ll_finish_md_op_data(op_data);
2065
2066         if ((file_lock->fl_flags & FL_FLOCK) &&
2067             (rc == 0 || file_lock->fl_type == F_UNLCK))
2068                 ll_flock_lock_file_wait(file, file_lock, (cmd == F_SETLKW));
2069 #ifdef HAVE_F_OP_FLOCK
2070         if ((file_lock->fl_flags & FL_POSIX) &&
2071             (rc == 0 || file_lock->fl_type == F_UNLCK) &&
2072             !(flags & LDLM_FL_TEST_LOCK))
2073                 posix_lock_file_wait(file, file_lock);
2074 #endif
2075
2076         RETURN(rc);
2077 }
2078
2079 int ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
2080 {
2081         ENTRY;
2082
2083         RETURN(-ENOSYS);
2084 }
2085
2086 int ll_have_md_lock(struct inode *inode, __u64 bits,  ldlm_mode_t l_req_mode)
2087 {
2088         struct lustre_handle lockh;
2089         ldlm_policy_data_t policy = { .l_inodebits = {bits}};
2090         ldlm_mode_t mode = (l_req_mode == LCK_MINMODE) ?
2091                                 (LCK_CR|LCK_CW|LCK_PR|LCK_PW) : l_req_mode;
2092         struct lu_fid *fid;
2093         int flags;
2094         ENTRY;
2095
2096         if (!inode)
2097                RETURN(0);
2098
2099         fid = &ll_i2info(inode)->lli_fid;
2100         CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
2101                ldlm_lockname[mode]);
2102
2103         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
2104         if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
2105                           mode, &lockh)) {
2106                 RETURN(1);
2107         }
2108         RETURN(0);
2109 }
2110
2111 ldlm_mode_t ll_take_md_lock(struct inode *inode, __u64 bits,
2112                             struct lustre_handle *lockh)
2113 {
2114         ldlm_policy_data_t policy = { .l_inodebits = {bits}};
2115         struct lu_fid *fid;
2116         ldlm_mode_t rc;
2117         int flags;
2118         ENTRY;
2119
2120         fid = &ll_i2info(inode)->lli_fid;
2121         CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
2122
2123         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
2124         rc = md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
2125                            LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
2126         RETURN(rc);
2127 }
2128
2129 static int ll_inode_revalidate_fini(struct inode *inode, int rc) {
2130         if (rc == -ENOENT) { /* Already unlinked. Just update nlink
2131                               * and return success */
2132                 inode->i_nlink = 0;
2133                 /* This path cannot be hit for regular files unless in
2134                  * case of obscure races, so no need to to validate
2135                  * size. */
2136                 if (!S_ISREG(inode->i_mode) &&
2137                     !S_ISDIR(inode->i_mode))
2138                         return 0;
2139         }
2140
2141         if (rc) {
2142                 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2143                 return -abs(rc);
2144
2145         }
2146
2147         return 0;
2148 }
2149
2150 int __ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2151                              __u64 ibits)
2152 {
2153         struct inode *inode = dentry->d_inode;
2154         struct ptlrpc_request *req = NULL;
2155         struct ll_sb_info *sbi;
2156         struct obd_export *exp;
2157         int rc = 0;
2158         ENTRY;
2159
2160         if (!inode) {
2161                 CERROR("REPORT THIS LINE TO PETER\n");
2162                 RETURN(0);
2163         }
2164         sbi = ll_i2sbi(inode);
2165
2166         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
2167                inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
2168
2169         exp = ll_i2mdexp(inode);
2170
2171         if (exp->exp_connect_flags & OBD_CONNECT_ATTRFID) {
2172                 struct lookup_intent oit = { .it_op = IT_GETATTR };
2173                 struct md_op_data *op_data;
2174
2175                 /* Call getattr by fid, so do not provide name at all. */
2176                 op_data = ll_prep_md_op_data(NULL, dentry->d_parent->d_inode,
2177                                              dentry->d_inode, NULL, 0, 0,
2178                                              LUSTRE_OPC_ANY, NULL);
2179                 if (IS_ERR(op_data))
2180                         RETURN(PTR_ERR(op_data));
2181
2182                 oit.it_create_mode |= M_CHECK_STALE;
2183                 rc = md_intent_lock(exp, op_data, NULL, 0,
2184                                     /* we are not interested in name
2185                                        based lookup */
2186                                     &oit, 0, &req,
2187                                     ll_md_blocking_ast, 0);
2188                 ll_finish_md_op_data(op_data);
2189                 oit.it_create_mode &= ~M_CHECK_STALE;
2190                 if (rc < 0) {
2191                         rc = ll_inode_revalidate_fini(inode, rc);
2192                         GOTO (out, rc);
2193                 }
2194
2195                 rc = ll_revalidate_it_finish(req, &oit, dentry);
2196                 if (rc != 0) {
2197                         ll_intent_release(&oit);
2198                         GOTO(out, rc);
2199                 }
2200
2201                 /* Unlinked? Unhash dentry, so it is not picked up later by
2202                    do_lookup() -> ll_revalidate_it(). We cannot use d_drop
2203                    here to preserve get_cwd functionality on 2.6.
2204                    Bug 10503 */
2205                 if (!dentry->d_inode->i_nlink) {
2206                         cfs_spin_lock(&ll_lookup_lock);
2207                         spin_lock(&dcache_lock);
2208                         ll_drop_dentry(dentry);
2209                         spin_unlock(&dcache_lock);
2210                         cfs_spin_unlock(&ll_lookup_lock);
2211                 }
2212
2213                 ll_lookup_finish_locks(&oit, dentry);
2214         } else if (!ll_have_md_lock(dentry->d_inode, ibits, LCK_MINMODE)) {
2215                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
2216                 obd_valid valid = OBD_MD_FLGETATTR;
2217                 struct md_op_data *op_data;
2218                 int ealen = 0;
2219
2220                 if (S_ISREG(inode->i_mode)) {
2221                         rc = ll_get_max_mdsize(sbi, &ealen);
2222                         if (rc)
2223                                 RETURN(rc);
2224                         valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
2225                 }
2226
2227                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2228                                              0, ealen, LUSTRE_OPC_ANY,
2229                                              NULL);
2230                 if (op_data == NULL)
2231                         RETURN(-ENOMEM);
2232
2233                 op_data->op_valid = valid;
2234                 /* Once OBD_CONNECT_ATTRFID is not supported, we can't find one
2235                  * capa for this inode. Because we only keep capas of dirs
2236                  * fresh. */
2237                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2238                 ll_finish_md_op_data(op_data);
2239                 if (rc) {
2240                         rc = ll_inode_revalidate_fini(inode, rc);
2241                         RETURN(rc);
2242                 }
2243
2244                 rc = ll_prep_inode(&inode, req, NULL);
2245         }
2246 out:
2247         ptlrpc_req_finished(req);
2248         return rc;
2249 }
2250
2251 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
2252 {
2253         struct inode *inode = dentry->d_inode;
2254         int rc;
2255         ENTRY;
2256
2257         rc = __ll_inode_revalidate_it(dentry, it, MDS_INODELOCK_UPDATE |
2258                                                   MDS_INODELOCK_LOOKUP);
2259
2260         /* if object not yet allocated, don't validate size */
2261         if (rc == 0 && ll_i2info(dentry->d_inode)->lli_smd == NULL) {
2262                 LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_lvb.lvb_atime;
2263                 LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_lvb.lvb_mtime;
2264                 LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_lvb.lvb_ctime;
2265                 RETURN(0);
2266         }
2267
2268         /* cl_glimpse_size will prefer locally cached writes if they extend
2269          * the file */
2270
2271         if (rc == 0)
2272                 rc = cl_glimpse_size(inode);
2273
2274         RETURN(rc);
2275 }
2276
2277 int ll_getattr_it(struct vfsmount *mnt, struct dentry *de,
2278                   struct lookup_intent *it, struct kstat *stat)
2279 {
2280         struct inode *inode = de->d_inode;
2281         struct ll_inode_info *lli = ll_i2info(inode);
2282         int res = 0;
2283
2284         res = ll_inode_revalidate_it(de, it);
2285         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETATTR, 1);
2286
2287         if (res)
2288                 return res;
2289
2290         stat->dev = inode->i_sb->s_dev;
2291         if (ll_need_32bit_api(ll_i2sbi(inode)))
2292                 stat->ino = cl_fid_build_ino32(&lli->lli_fid);
2293         else
2294                 stat->ino = inode->i_ino;
2295
2296         stat->mode = inode->i_mode;
2297         stat->nlink = inode->i_nlink;
2298         stat->uid = inode->i_uid;
2299         stat->gid = inode->i_gid;
2300         stat->rdev = kdev_t_to_nr(inode->i_rdev);
2301         stat->atime = inode->i_atime;
2302         stat->mtime = inode->i_mtime;
2303         stat->ctime = inode->i_ctime;
2304 #ifdef HAVE_INODE_BLKSIZE
2305         stat->blksize = inode->i_blksize;
2306 #else
2307         stat->blksize = 1 << inode->i_blkbits;
2308 #endif
2309
2310         stat->size = i_size_read(inode);
2311         stat->blocks = inode->i_blocks;
2312
2313         return 0;
2314 }
2315 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
2316 {
2317         struct lookup_intent it = { .it_op = IT_GETATTR };
2318
2319         return ll_getattr_it(mnt, de, &it, stat);
2320 }
2321
2322 #ifdef HAVE_LINUX_FIEMAP_H
2323 int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2324                 __u64 start, __u64 len)
2325 {
2326         int rc;
2327         size_t num_bytes;
2328         struct ll_user_fiemap *fiemap;
2329         unsigned int extent_count = fieinfo->fi_extents_max;
2330
2331         num_bytes = sizeof(*fiemap) + (extent_count *
2332                                        sizeof(struct ll_fiemap_extent));
2333         OBD_VMALLOC(fiemap, num_bytes);
2334
2335         if (fiemap == NULL)
2336                 RETURN(-ENOMEM);
2337
2338         fiemap->fm_flags = fieinfo->fi_flags;
2339         fiemap->fm_extent_count = fieinfo->fi_extents_max;
2340         fiemap->fm_start = start;
2341         fiemap->fm_length = len;
2342         memcpy(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
2343                sizeof(struct ll_fiemap_extent));
2344
2345         rc = ll_do_fiemap(inode, fiemap, num_bytes);
2346
2347         fieinfo->fi_flags = fiemap->fm_flags;
2348         fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
2349         memcpy(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
2350                fiemap->fm_mapped_extents * sizeof(struct ll_fiemap_extent));
2351
2352         OBD_VFREE(fiemap, num_bytes);
2353         return rc;
2354 }
2355 #endif
2356
2357
2358 static
2359 int lustre_check_acl(struct inode *inode, int mask)
2360 {
2361 #ifdef CONFIG_FS_POSIX_ACL
2362         struct ll_inode_info *lli = ll_i2info(inode);
2363         struct posix_acl *acl;
2364         int rc;
2365         ENTRY;
2366
2367         cfs_spin_lock(&lli->lli_lock);
2368         acl = posix_acl_dup(lli->lli_posix_acl);
2369         cfs_spin_unlock(&lli->lli_lock);
2370
2371         if (!acl)
2372                 RETURN(-EAGAIN);
2373
2374         rc = posix_acl_permission(inode, acl, mask);
2375         posix_acl_release(acl);
2376
2377         RETURN(rc);
2378 #else
2379         return -EAGAIN;
2380 #endif
2381 }
2382
2383 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
2384 #ifndef HAVE_INODE_PERMISION_2ARGS
2385 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2386 #else
2387 int ll_inode_permission(struct inode *inode, int mask)
2388 #endif
2389 {
2390         int rc = 0;
2391         ENTRY;
2392
2393        /* as root inode are NOT getting validated in lookup operation,
2394         * need to do it before permission check. */
2395
2396         if (inode == inode->i_sb->s_root->d_inode) {
2397                 struct lookup_intent it = { .it_op = IT_LOOKUP };
2398
2399                 rc = __ll_inode_revalidate_it(inode->i_sb->s_root, &it,
2400                                               MDS_INODELOCK_LOOKUP);
2401                 if (rc)
2402                         RETURN(rc);
2403         }
2404
2405         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), inode mode %x mask %o\n",
2406                inode->i_ino, inode->i_generation, inode, inode->i_mode, mask);
2407
2408         if (ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT)
2409                 return lustre_check_remote_perm(inode, mask);
2410
2411         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
2412         rc = generic_permission(inode, mask, lustre_check_acl);
2413
2414         RETURN(rc);
2415 }
2416 #else
2417 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2418 {
2419         int mode = inode->i_mode;
2420         int rc;
2421
2422         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), mask %o\n",
2423                inode->i_ino, inode->i_generation, inode, mask);
2424
2425         if (ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT)
2426                 return lustre_check_remote_perm(inode, mask);
2427
2428         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
2429
2430         if ((mask & MAY_WRITE) && IS_RDONLY(inode) &&
2431             (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2432                 return -EROFS;
2433         if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
2434                 return -EACCES;
2435         if (cfs_curproc_fsuid() == inode->i_uid) {
2436                 mode >>= 6;
2437         } else if (1) {
2438                 if (((mode >> 3) & mask & S_IRWXO) != mask)
2439                         goto check_groups;
2440                 rc = lustre_check_acl(inode, mask);
2441                 if (rc == -EAGAIN)
2442                         goto check_groups;
2443                 if (rc == -EACCES)
2444                         goto check_capabilities;
2445                 return rc;
2446         } else {
2447 check_groups:
2448                 if (cfs_curproc_is_in_groups(inode->i_gid))
2449                         mode >>= 3;
2450         }
2451         if ((mode & mask & S_IRWXO) == mask)
2452                 return 0;
2453
2454 check_capabilities:
2455         if (!(mask & MAY_EXEC) ||
2456             (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
2457                 if (cfs_capable(CFS_CAP_DAC_OVERRIDE))
2458                         return 0;
2459
2460         if (cfs_capable(CFS_CAP_DAC_READ_SEARCH) && ((mask == MAY_READ) ||
2461             (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))))
2462                 return 0;
2463
2464         return -EACCES;
2465 }
2466 #endif
2467
2468 #ifdef HAVE_FILE_READV
2469 #define READ_METHOD readv
2470 #define READ_FUNCTION ll_file_readv
2471 #define WRITE_METHOD writev
2472 #define WRITE_FUNCTION ll_file_writev
2473 #else
2474 #define READ_METHOD aio_read
2475 #define READ_FUNCTION ll_file_aio_read
2476 #define WRITE_METHOD aio_write
2477 #define WRITE_FUNCTION ll_file_aio_write
2478 #endif
2479
2480 /* -o localflock - only provides locally consistent flock locks */
2481 struct file_operations ll_file_operations = {
2482         .read           = ll_file_read,
2483         .READ_METHOD    = READ_FUNCTION,
2484         .write          = ll_file_write,
2485         .WRITE_METHOD   = WRITE_FUNCTION,
2486         .ioctl          = ll_file_ioctl,
2487         .open           = ll_file_open,
2488         .release        = ll_file_release,
2489         .mmap           = ll_file_mmap,
2490         .llseek         = ll_file_seek,
2491 #ifdef HAVE_KERNEL_SENDFILE
2492         .sendfile       = ll_file_sendfile,
2493 #endif
2494 #ifdef HAVE_KERNEL_SPLICE_READ
2495         .splice_read    = ll_file_splice_read,
2496 #endif
2497         .fsync          = ll_fsync,
2498         .flush          = ll_flush
2499 };
2500
2501 struct file_operations ll_file_operations_flock = {
2502         .read           = ll_file_read,
2503         .READ_METHOD    = READ_FUNCTION,
2504         .write          = ll_file_write,
2505         .WRITE_METHOD   = WRITE_FUNCTION,
2506         .ioctl          = ll_file_ioctl,
2507         .open           = ll_file_open,
2508         .release        = ll_file_release,
2509         .mmap           = ll_file_mmap,
2510         .llseek         = ll_file_seek,
2511 #ifdef HAVE_KERNEL_SENDFILE
2512         .sendfile       = ll_file_sendfile,
2513 #endif
2514 #ifdef HAVE_KERNEL_SPLICE_READ
2515         .splice_read    = ll_file_splice_read,
2516 #endif
2517         .fsync          = ll_fsync,
2518         .flush          = ll_flush,
2519 #ifdef HAVE_F_OP_FLOCK
2520         .flock          = ll_file_flock,
2521 #endif
2522         .lock           = ll_file_flock
2523 };
2524
2525 /* These are for -o noflock - to return ENOSYS on flock calls */
2526 struct file_operations ll_file_operations_noflock = {
2527         .read           = ll_file_read,
2528         .READ_METHOD    = READ_FUNCTION,
2529         .write          = ll_file_write,
2530         .WRITE_METHOD   = WRITE_FUNCTION,
2531         .ioctl          = ll_file_ioctl,
2532         .open           = ll_file_open,
2533         .release        = ll_file_release,
2534         .mmap           = ll_file_mmap,
2535         .llseek         = ll_file_seek,
2536 #ifdef HAVE_KERNEL_SENDFILE
2537         .sendfile       = ll_file_sendfile,
2538 #endif
2539 #ifdef HAVE_KERNEL_SPLICE_READ
2540         .splice_read    = ll_file_splice_read,
2541 #endif
2542         .fsync          = ll_fsync,
2543         .flush          = ll_flush,
2544 #ifdef HAVE_F_OP_FLOCK
2545         .flock          = ll_file_noflock,
2546 #endif
2547         .lock           = ll_file_noflock
2548 };
2549
2550 struct inode_operations ll_file_inode_operations = {
2551 #ifdef HAVE_VFS_INTENT_PATCHES
2552         .setattr_raw    = ll_setattr_raw,
2553 #endif
2554         .setattr        = ll_setattr,
2555         .truncate       = ll_truncate,
2556         .getattr        = ll_getattr,
2557         .permission     = ll_inode_permission,
2558         .setxattr       = ll_setxattr,
2559         .getxattr       = ll_getxattr,
2560         .listxattr      = ll_listxattr,
2561         .removexattr    = ll_removexattr,
2562 #ifdef  HAVE_LINUX_FIEMAP_H
2563         .fiemap         = ll_fiemap,
2564 #endif
2565 };
2566
2567 /* dynamic ioctl number support routins */
2568 static struct llioc_ctl_data {
2569         cfs_rw_semaphore_t      ioc_sem;
2570         cfs_list_t              ioc_head;
2571 } llioc = {
2572         __RWSEM_INITIALIZER(llioc.ioc_sem),
2573         CFS_LIST_HEAD_INIT(llioc.ioc_head)
2574 };
2575
2576
2577 struct llioc_data {
2578         cfs_list_t              iocd_list;
2579         unsigned int            iocd_size;
2580         llioc_callback_t        iocd_cb;
2581         unsigned int            iocd_count;
2582         unsigned int            iocd_cmd[0];
2583 };
2584
2585 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd)
2586 {
2587         unsigned int size;
2588         struct llioc_data *in_data = NULL;
2589         ENTRY;
2590
2591         if (cb == NULL || cmd == NULL ||
2592             count > LLIOC_MAX_CMD || count < 0)
2593                 RETURN(NULL);
2594
2595         size = sizeof(*in_data) + count * sizeof(unsigned int);
2596         OBD_ALLOC(in_data, size);
2597         if (in_data == NULL)
2598                 RETURN(NULL);
2599
2600         memset(in_data, 0, sizeof(*in_data));
2601         in_data->iocd_size = size;
2602         in_data->iocd_cb = cb;
2603         in_data->iocd_count = count;
2604         memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count);
2605
2606         cfs_down_write(&llioc.ioc_sem);
2607         cfs_list_add_tail(&in_data->iocd_list, &llioc.ioc_head);
2608         cfs_up_write(&llioc.ioc_sem);
2609
2610         RETURN(in_data);
2611 }
2612
2613 void ll_iocontrol_unregister(void *magic)
2614 {
2615         struct llioc_data *tmp;
2616
2617         if (magic == NULL)
2618                 return;
2619
2620         cfs_down_write(&llioc.ioc_sem);
2621         cfs_list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) {
2622                 if (tmp == magic) {
2623                         unsigned int size = tmp->iocd_size;
2624
2625                         cfs_list_del(&tmp->iocd_list);
2626                         cfs_up_write(&llioc.ioc_sem);
2627
2628                         OBD_FREE(tmp, size);
2629                         return;
2630                 }
2631         }
2632         cfs_up_write(&llioc.ioc_sem);
2633
2634         CWARN("didn't find iocontrol register block with magic: %p\n", magic);
2635 }
2636
2637 EXPORT_SYMBOL(ll_iocontrol_register);
2638 EXPORT_SYMBOL(ll_iocontrol_unregister);
2639
2640 enum llioc_iter ll_iocontrol_call(struct inode *inode, struct file *file,
2641                         unsigned int cmd, unsigned long arg, int *rcp)
2642 {
2643         enum llioc_iter ret = LLIOC_CONT;
2644         struct llioc_data *data;
2645         int rc = -EINVAL, i;
2646
2647         cfs_down_read(&llioc.ioc_sem);
2648         cfs_list_for_each_entry(data, &llioc.ioc_head, iocd_list) {
2649                 for (i = 0; i < data->iocd_count; i++) {
2650                         if (cmd != data->iocd_cmd[i])
2651                                 continue;
2652
2653                         ret = data->iocd_cb(inode, file, cmd, arg, data, &rc);
2654                         break;
2655                 }
2656
2657                 if (ret == LLIOC_STOP)
2658                         break;
2659         }
2660         cfs_up_read(&llioc.ioc_sem);
2661
2662         if (rcp)
2663                 *rcp = rc;
2664         return ret;
2665 }