Whamcloud - gitweb
b=22342 process racer condition between statahead and rename/unlink operation
[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  2008 Sun Microsystems, Inc. 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         inode->i_blocks = lvb.lvb_blocks;
782
783         LTIME_S(inode->i_mtime) = lvb.lvb_mtime;
784         LTIME_S(inode->i_atime) = lvb.lvb_atime;
785         LTIME_S(inode->i_ctime) = lvb.lvb_ctime;
786         ll_inode_size_unlock(inode, 1);
787
788         RETURN(rc);
789 }
790
791 int ll_glimpse_ioctl(struct ll_sb_info *sbi, struct lov_stripe_md *lsm,
792                      lstat_t *st)
793 {
794         struct obdo obdo = { 0 };
795         int rc;
796
797         rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, NULL, &obdo, 0, 0);
798         if (rc == 0) {
799                 st->st_size   = obdo.o_size;
800                 st->st_blocks = obdo.o_blocks;
801                 st->st_mtime  = obdo.o_mtime;
802                 st->st_atime  = obdo.o_atime;
803                 st->st_ctime  = obdo.o_ctime;
804         }
805         return rc;
806 }
807
808 void ll_io_init(struct cl_io *io, const struct file *file, int write)
809 {
810         struct inode *inode = file->f_dentry->d_inode;
811
812         memset(io, 0, sizeof *io);
813         io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
814         if (write)
815                 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
816         io->ci_obj     = ll_i2info(inode)->lli_clob;
817         io->ci_lockreq = CILR_MAYBE;
818         if (ll_file_nolock(file)) {
819                 io->ci_lockreq = CILR_NEVER;
820                 io->ci_no_srvlock = 1;
821         } else if (file->f_flags & O_APPEND) {
822                 io->ci_lockreq = CILR_MANDATORY;
823         }
824 }
825
826 static ssize_t ll_file_io_generic(const struct lu_env *env,
827                 struct vvp_io_args *args, struct file *file,
828                 enum cl_io_type iot, loff_t *ppos, size_t count)
829 {
830         struct cl_io       *io;
831         ssize_t             result;
832         ENTRY;
833
834         io = &ccc_env_info(env)->cti_io;
835         ll_io_init(io, file, iot == CIT_WRITE);
836
837         if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
838                 struct vvp_io *vio = vvp_env_io(env);
839                 struct ccc_io *cio = ccc_env_io(env);
840                 struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
841                 int write_sem_locked = 0;
842
843                 cio->cui_fd  = LUSTRE_FPRIVATE(file);
844                 vio->cui_io_subtype = args->via_io_subtype;
845
846                 switch (vio->cui_io_subtype) {
847                 case IO_NORMAL:
848                         cio->cui_iov = args->u.normal.via_iov;
849                         cio->cui_nrsegs = args->u.normal.via_nrsegs;
850                         cio->cui_tot_nrsegs = cio->cui_nrsegs;
851 #ifndef HAVE_FILE_WRITEV
852                         cio->cui_iocb = args->u.normal.via_iocb;
853 #endif
854                         if ((iot == CIT_WRITE) &&
855                             !(cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
856                                 cfs_down(&lli->lli_write_sem);
857                                 write_sem_locked = 1;
858                         }
859                         break;
860                 case IO_SENDFILE:
861                         vio->u.sendfile.cui_actor = args->u.sendfile.via_actor;
862                         vio->u.sendfile.cui_target = args->u.sendfile.via_target;
863                         break;
864                 case IO_SPLICE:
865                         vio->u.splice.cui_pipe = args->u.splice.via_pipe;
866                         vio->u.splice.cui_flags = args->u.splice.via_flags;
867                         break;
868                 default:
869                         CERROR("Unknow IO type - %u\n", vio->cui_io_subtype);
870                         LBUG();
871                 }
872                 result = cl_io_loop(env, io);
873                 if (write_sem_locked)
874                         cfs_up(&lli->lli_write_sem);
875         } else {
876                 /* cl_io_rw_init() handled IO */
877                 result = io->ci_result;
878         }
879
880         if (io->ci_nob > 0) {
881                 result = io->ci_nob;
882                 *ppos = io->u.ci_wr.wr.crw_pos;
883         }
884         cl_io_fini(env, io);
885         RETURN(result);
886 }
887
888
889 /*
890  * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
891  */
892 static int ll_file_get_iov_count(const struct iovec *iov,
893                                  unsigned long *nr_segs, size_t *count)
894 {
895         size_t cnt = 0;
896         unsigned long seg;
897
898         for (seg = 0; seg < *nr_segs; seg++) {
899                 const struct iovec *iv = &iov[seg];
900
901                 /*
902                  * If any segment has a negative length, or the cumulative
903                  * length ever wraps negative then return -EINVAL.
904                  */
905                 cnt += iv->iov_len;
906                 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
907                         return -EINVAL;
908                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
909                         continue;
910                 if (seg == 0)
911                         return -EFAULT;
912                 *nr_segs = seg;
913                 cnt -= iv->iov_len;   /* This segment is no good */
914                 break;
915         }
916         *count = cnt;
917         return 0;
918 }
919
920 #ifdef HAVE_FILE_READV
921 static ssize_t ll_file_readv(struct file *file, const struct iovec *iov,
922                               unsigned long nr_segs, loff_t *ppos)
923 {
924         struct lu_env      *env;
925         struct vvp_io_args *args;
926         size_t              count;
927         ssize_t             result;
928         int                 refcheck;
929         ENTRY;
930
931         result = ll_file_get_iov_count(iov, &nr_segs, &count);
932         if (result)
933                 RETURN(result);
934
935         env = cl_env_get(&refcheck);
936         if (IS_ERR(env))
937                 RETURN(PTR_ERR(env));
938
939         args = vvp_env_args(env, IO_NORMAL);
940         args->u.normal.via_iov = (struct iovec *)iov;
941         args->u.normal.via_nrsegs = nr_segs;
942
943         result = ll_file_io_generic(env, args, file, CIT_READ, ppos, count);
944         cl_env_put(env, &refcheck);
945         RETURN(result);
946 }
947
948 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
949                             loff_t *ppos)
950 {
951         struct lu_env *env;
952         struct iovec  *local_iov;
953         ssize_t        result;
954         int            refcheck;
955         ENTRY;
956
957         env = cl_env_get(&refcheck);
958         if (IS_ERR(env))
959                 RETURN(PTR_ERR(env));
960
961         local_iov = &vvp_env_info(env)->vti_local_iov;
962         local_iov->iov_base = (void __user *)buf;
963         local_iov->iov_len = count;
964         result = ll_file_readv(file, local_iov, 1, ppos);
965         cl_env_put(env, &refcheck);
966         RETURN(result);
967 }
968
969 #else
970 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
971                                 unsigned long nr_segs, loff_t pos)
972 {
973         struct lu_env      *env;
974         struct vvp_io_args *args;
975         size_t              count;
976         ssize_t             result;
977         int                 refcheck;
978         ENTRY;
979
980         result = ll_file_get_iov_count(iov, &nr_segs, &count);
981         if (result)
982                 RETURN(result);
983
984         env = cl_env_get(&refcheck);
985         if (IS_ERR(env))
986                 RETURN(PTR_ERR(env));
987
988         args = vvp_env_args(env, IO_NORMAL);
989         args->u.normal.via_iov = (struct iovec *)iov;
990         args->u.normal.via_nrsegs = nr_segs;
991         args->u.normal.via_iocb = iocb;
992
993         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
994                                     &iocb->ki_pos, count);
995         cl_env_put(env, &refcheck);
996         RETURN(result);
997 }
998
999 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
1000                             loff_t *ppos)
1001 {
1002         struct lu_env *env;
1003         struct iovec  *local_iov;
1004         struct kiocb  *kiocb;
1005         ssize_t        result;
1006         int            refcheck;
1007         ENTRY;
1008
1009         env = cl_env_get(&refcheck);
1010         if (IS_ERR(env))
1011                 RETURN(PTR_ERR(env));
1012
1013         local_iov = &vvp_env_info(env)->vti_local_iov;
1014         kiocb = &vvp_env_info(env)->vti_kiocb;
1015         local_iov->iov_base = (void __user *)buf;
1016         local_iov->iov_len = count;
1017         init_sync_kiocb(kiocb, file);
1018         kiocb->ki_pos = *ppos;
1019         kiocb->ki_left = count;
1020
1021         result = ll_file_aio_read(kiocb, local_iov, 1, kiocb->ki_pos);
1022         *ppos = kiocb->ki_pos;
1023
1024         cl_env_put(env, &refcheck);
1025         RETURN(result);
1026 }
1027 #endif
1028
1029 /*
1030  * Write to a file (through the page cache).
1031  */
1032 #ifdef HAVE_FILE_WRITEV
1033 static ssize_t ll_file_writev(struct file *file, const struct iovec *iov,
1034                               unsigned long nr_segs, loff_t *ppos)
1035 {
1036         struct lu_env      *env;
1037         struct vvp_io_args *args;
1038         size_t              count;
1039         ssize_t             result;
1040         int                 refcheck;
1041         ENTRY;
1042
1043         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1044         if (result)
1045                 RETURN(result);
1046
1047         env = cl_env_get(&refcheck);
1048         if (IS_ERR(env))
1049                 RETURN(PTR_ERR(env));
1050
1051         args = vvp_env_args(env, IO_NORMAL);
1052         args->u.normal.via_iov = (struct iovec *)iov;
1053         args->u.normal.via_nrsegs = nr_segs;
1054
1055         result = ll_file_io_generic(env, args, file, CIT_WRITE, ppos, count);
1056         cl_env_put(env, &refcheck);
1057         RETURN(result);
1058 }
1059
1060 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1061                              loff_t *ppos)
1062 {
1063         struct lu_env    *env;
1064         struct iovec     *local_iov;
1065         ssize_t           result;
1066         int               refcheck;
1067         ENTRY;
1068
1069         env = cl_env_get(&refcheck);
1070         if (IS_ERR(env))
1071                 RETURN(PTR_ERR(env));
1072
1073         local_iov = &vvp_env_info(env)->vti_local_iov;
1074         local_iov->iov_base = (void __user *)buf;
1075         local_iov->iov_len = count;
1076
1077         result = ll_file_writev(file, local_iov, 1, ppos);
1078         cl_env_put(env, &refcheck);
1079         RETURN(result);
1080 }
1081
1082 #else /* AIO stuff */
1083 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1084                                  unsigned long nr_segs, loff_t pos)
1085 {
1086         struct lu_env      *env;
1087         struct vvp_io_args *args;
1088         size_t              count;
1089         ssize_t             result;
1090         int                 refcheck;
1091         ENTRY;
1092
1093         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1094         if (result)
1095                 RETURN(result);
1096
1097         env = cl_env_get(&refcheck);
1098         if (IS_ERR(env))
1099                 RETURN(PTR_ERR(env));
1100
1101         args = vvp_env_args(env, IO_NORMAL);
1102         args->u.normal.via_iov = (struct iovec *)iov;
1103         args->u.normal.via_nrsegs = nr_segs;
1104         args->u.normal.via_iocb = iocb;
1105
1106         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1107                                   &iocb->ki_pos, count);
1108         cl_env_put(env, &refcheck);
1109         RETURN(result);
1110 }
1111
1112 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1113                              loff_t *ppos)
1114 {
1115         struct lu_env *env;
1116         struct iovec  *local_iov;
1117         struct kiocb  *kiocb;
1118         ssize_t        result;
1119         int            refcheck;
1120         ENTRY;
1121
1122         env = cl_env_get(&refcheck);
1123         if (IS_ERR(env))
1124                 RETURN(PTR_ERR(env));
1125
1126         local_iov = &vvp_env_info(env)->vti_local_iov;
1127         kiocb = &vvp_env_info(env)->vti_kiocb;
1128         local_iov->iov_base = (void __user *)buf;
1129         local_iov->iov_len = count;
1130         init_sync_kiocb(kiocb, file);
1131         kiocb->ki_pos = *ppos;
1132         kiocb->ki_left = count;
1133
1134         result = ll_file_aio_write(kiocb, local_iov, 1, kiocb->ki_pos);
1135         *ppos = kiocb->ki_pos;
1136
1137         cl_env_put(env, &refcheck);
1138         RETURN(result);
1139 }
1140 #endif
1141
1142
1143 #ifdef HAVE_KERNEL_SENDFILE
1144 /*
1145  * Send file content (through pagecache) somewhere with helper
1146  */
1147 static ssize_t ll_file_sendfile(struct file *in_file, loff_t *ppos,size_t count,
1148                                 read_actor_t actor, void *target)
1149 {
1150         struct lu_env      *env;
1151         struct vvp_io_args *args;
1152         ssize_t             result;
1153         int                 refcheck;
1154         ENTRY;
1155
1156         env = cl_env_get(&refcheck);
1157         if (IS_ERR(env))
1158                 RETURN(PTR_ERR(env));
1159
1160         args = vvp_env_args(env, IO_SENDFILE);
1161         args->u.sendfile.via_target = target;
1162         args->u.sendfile.via_actor = actor;
1163
1164         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1165         cl_env_put(env, &refcheck);
1166         RETURN(result);
1167 }
1168 #endif
1169
1170 #ifdef HAVE_KERNEL_SPLICE_READ
1171 /*
1172  * Send file content (through pagecache) somewhere with helper
1173  */
1174 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1175                                    struct pipe_inode_info *pipe, size_t count,
1176                                    unsigned int flags)
1177 {
1178         struct lu_env      *env;
1179         struct vvp_io_args *args;
1180         ssize_t             result;
1181         int                 refcheck;
1182         ENTRY;
1183
1184         env = cl_env_get(&refcheck);
1185         if (IS_ERR(env))
1186                 RETURN(PTR_ERR(env));
1187
1188         args = vvp_env_args(env, IO_SPLICE);
1189         args->u.splice.via_pipe = pipe;
1190         args->u.splice.via_flags = flags;
1191
1192         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1193         cl_env_put(env, &refcheck);
1194         RETURN(result);
1195 }
1196 #endif
1197
1198 static int ll_lov_recreate_obj(struct inode *inode, struct file *file,
1199                                unsigned long arg)
1200 {
1201         struct obd_export *exp = ll_i2dtexp(inode);
1202         struct ll_recreate_obj ucreatp;
1203         struct obd_trans_info oti = { 0 };
1204         struct obdo *oa = NULL;
1205         int lsm_size;
1206         int rc = 0;
1207         struct lov_stripe_md *lsm, *lsm2;
1208         ENTRY;
1209
1210         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1211                 RETURN(-EPERM);
1212
1213         if (cfs_copy_from_user(&ucreatp, (struct ll_recreate_obj *)arg,
1214                                sizeof(struct ll_recreate_obj)))
1215                 RETURN(-EFAULT);
1216
1217         OBDO_ALLOC(oa);
1218         if (oa == NULL)
1219                 RETURN(-ENOMEM);
1220
1221         ll_inode_size_lock(inode, 0);
1222         lsm = ll_i2info(inode)->lli_smd;
1223         if (lsm == NULL)
1224                 GOTO(out, rc = -ENOENT);
1225         lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
1226                    (lsm->lsm_stripe_count));
1227
1228         OBD_ALLOC(lsm2, lsm_size);
1229         if (lsm2 == NULL)
1230                 GOTO(out, rc = -ENOMEM);
1231
1232         oa->o_id = ucreatp.lrc_id;
1233         oa->o_seq = ucreatp.lrc_seq;
1234         oa->o_nlink = ucreatp.lrc_ost_idx;
1235         oa->o_flags |= OBD_FL_RECREATE_OBJS;
1236         oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1237         obdo_from_inode(oa, inode, &ll_i2info(inode)->lli_fid, OBD_MD_FLTYPE |
1238                         OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1239         memcpy(lsm2, lsm, lsm_size);
1240         rc = obd_create(exp, oa, &lsm2, &oti);
1241
1242         OBD_FREE(lsm2, lsm_size);
1243         GOTO(out, rc);
1244 out:
1245         ll_inode_size_unlock(inode, 0);
1246         OBDO_FREE(oa);
1247         return rc;
1248 }
1249
1250 int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1251                              int flags, struct lov_user_md *lum, int lum_size)
1252 {
1253         struct lov_stripe_md *lsm;
1254         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1255         int rc = 0;
1256         ENTRY;
1257
1258         ll_inode_size_lock(inode, 0);
1259         lsm = ll_i2info(inode)->lli_smd;
1260         if (lsm) {
1261                 ll_inode_size_unlock(inode, 0);
1262                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1263                        inode->i_ino);
1264                 RETURN(-EEXIST);
1265         }
1266
1267         rc = ll_intent_file_open(file, lum, lum_size, &oit);
1268         if (rc)
1269                 GOTO(out, rc);
1270         if (it_disposition(&oit, DISP_LOOKUP_NEG))
1271                 GOTO(out_req_free, rc = -ENOENT);
1272         rc = oit.d.lustre.it_status;
1273         if (rc < 0)
1274                 GOTO(out_req_free, rc);
1275
1276         ll_release_openhandle(file->f_dentry, &oit);
1277
1278  out:
1279         ll_inode_size_unlock(inode, 0);
1280         ll_intent_release(&oit);
1281         RETURN(rc);
1282 out_req_free:
1283         ptlrpc_req_finished((struct ptlrpc_request *) oit.d.lustre.it_data);
1284         goto out;
1285 }
1286
1287 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1288                              struct lov_mds_md **lmmp, int *lmm_size,
1289                              struct ptlrpc_request **request)
1290 {
1291         struct ll_sb_info *sbi = ll_i2sbi(inode);
1292         struct mdt_body  *body;
1293         struct lov_mds_md *lmm = NULL;
1294         struct ptlrpc_request *req = NULL;
1295         struct md_op_data *op_data;
1296         int rc, lmmsize;
1297
1298         rc = ll_get_max_mdsize(sbi, &lmmsize);
1299         if (rc)
1300                 RETURN(rc);
1301
1302         op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1303                                      strlen(filename), lmmsize,
1304                                      LUSTRE_OPC_ANY, NULL);
1305         if (op_data == NULL)
1306                 RETURN(-ENOMEM);
1307
1308         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1309         rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1310         ll_finish_md_op_data(op_data);
1311         if (rc < 0) {
1312                 CDEBUG(D_INFO, "md_getattr_name failed "
1313                        "on %s: rc %d\n", filename, rc);
1314                 GOTO(out, rc);
1315         }
1316
1317         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1318         LASSERT(body != NULL); /* checked by mdc_getattr_name */
1319
1320         lmmsize = body->eadatasize;
1321
1322         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1323                         lmmsize == 0) {
1324                 GOTO(out, rc = -ENODATA);
1325         }
1326
1327         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1328         LASSERT(lmm != NULL);
1329
1330         if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) &&
1331             (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) {
1332                 GOTO(out, rc = -EPROTO);
1333         }
1334
1335         /*
1336          * This is coming from the MDS, so is probably in
1337          * little endian.  We convert it to host endian before
1338          * passing it to userspace.
1339          */
1340         if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
1341                 /* if function called for directory - we should
1342                  * avoid swab not existent lsm objects */
1343                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1344                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1345                         if (S_ISREG(body->mode))
1346                                 lustre_swab_lov_user_md_objects(
1347                                  ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1348                                  ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1349                 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1350                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1351                         if (S_ISREG(body->mode))
1352                                 lustre_swab_lov_user_md_objects(
1353                                  ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1354                                  ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1355                 }
1356         }
1357
1358 out:
1359         *lmmp = lmm;
1360         *lmm_size = lmmsize;
1361         *request = req;
1362         return rc;
1363 }
1364
1365 static int ll_lov_setea(struct inode *inode, struct file *file,
1366                             unsigned long arg)
1367 {
1368         int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1369         struct lov_user_md  *lump;
1370         int lum_size = sizeof(struct lov_user_md) +
1371                        sizeof(struct lov_user_ost_data);
1372         int rc;
1373         ENTRY;
1374
1375         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1376                 RETURN(-EPERM);
1377
1378         OBD_ALLOC(lump, lum_size);
1379         if (lump == NULL) {
1380                 RETURN(-ENOMEM);
1381         }
1382         if (cfs_copy_from_user(lump, (struct lov_user_md  *)arg, lum_size)) {
1383                 OBD_FREE(lump, lum_size);
1384                 RETURN(-EFAULT);
1385         }
1386
1387         rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1388
1389         OBD_FREE(lump, lum_size);
1390         RETURN(rc);
1391 }
1392
1393 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1394                             unsigned long arg)
1395 {
1396         struct lov_user_md_v3 lumv3;
1397         struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1398         struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1399         struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1400         int lum_size;
1401         int rc;
1402         int flags = FMODE_WRITE;
1403         ENTRY;
1404
1405         /* first try with v1 which is smaller than v3 */
1406         lum_size = sizeof(struct lov_user_md_v1);
1407         if (cfs_copy_from_user(lumv1, lumv1p, lum_size))
1408                 RETURN(-EFAULT);
1409
1410         if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1411                 lum_size = sizeof(struct lov_user_md_v3);
1412                 if (cfs_copy_from_user(&lumv3, lumv3p, lum_size))
1413                         RETURN(-EFAULT);
1414         }
1415
1416         rc = ll_lov_setstripe_ea_info(inode, file, flags, lumv1, lum_size);
1417         if (rc == 0) {
1418                  put_user(0, &lumv1p->lmm_stripe_count);
1419                  rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1420                                     0, ll_i2info(inode)->lli_smd,
1421                                     (void *)arg);
1422         }
1423         RETURN(rc);
1424 }
1425
1426 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1427 {
1428         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1429
1430         if (!lsm)
1431                 RETURN(-ENODATA);
1432
1433         return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0, lsm,
1434                             (void *)arg);
1435 }
1436
1437 int ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1438 {
1439         struct ll_inode_info   *lli = ll_i2info(inode);
1440         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1441         struct ccc_grouplock    grouplock;
1442         int                     rc;
1443         ENTRY;
1444
1445         if (ll_file_nolock(file))
1446                 RETURN(-EOPNOTSUPP);
1447
1448         cfs_spin_lock(&lli->lli_lock);
1449         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1450                 CWARN("group lock already existed with gid %lu\n",
1451                        fd->fd_grouplock.cg_gid);
1452                 cfs_spin_unlock(&lli->lli_lock);
1453                 RETURN(-EINVAL);
1454         }
1455         LASSERT(fd->fd_grouplock.cg_lock == NULL);
1456         cfs_spin_unlock(&lli->lli_lock);
1457
1458         rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1459                               arg, (file->f_flags & O_NONBLOCK), &grouplock);
1460         if (rc)
1461                 RETURN(rc);
1462
1463         cfs_spin_lock(&lli->lli_lock);
1464         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1465                 cfs_spin_unlock(&lli->lli_lock);
1466                 CERROR("another thread just won the race\n");
1467                 cl_put_grouplock(&grouplock);
1468                 RETURN(-EINVAL);
1469         }
1470
1471         fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1472         fd->fd_grouplock = grouplock;
1473         cfs_spin_unlock(&lli->lli_lock);
1474
1475         CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
1476         RETURN(0);
1477 }
1478
1479 int ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1480 {
1481         struct ll_inode_info   *lli = ll_i2info(inode);
1482         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1483         struct ccc_grouplock    grouplock;
1484         ENTRY;
1485
1486         cfs_spin_lock(&lli->lli_lock);
1487         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1488                 cfs_spin_unlock(&lli->lli_lock);
1489                 CWARN("no group lock held\n");
1490                 RETURN(-EINVAL);
1491         }
1492         LASSERT(fd->fd_grouplock.cg_lock != NULL);
1493
1494         if (fd->fd_grouplock.cg_gid != arg) {
1495                 CWARN("group lock %lu doesn't match current id %lu\n",
1496                        arg, fd->fd_grouplock.cg_gid);
1497                 cfs_spin_unlock(&lli->lli_lock);
1498                 RETURN(-EINVAL);
1499         }
1500
1501         grouplock = fd->fd_grouplock;
1502         memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1503         fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1504         cfs_spin_unlock(&lli->lli_lock);
1505
1506         cl_put_grouplock(&grouplock);
1507         CDEBUG(D_INFO, "group lock %lu released\n", arg);
1508         RETURN(0);
1509 }
1510
1511 /**
1512  * Close inode open handle
1513  *
1514  * \param dentry [in]     dentry which contains the inode
1515  * \param it     [in,out] intent which contains open info and result
1516  *
1517  * \retval 0     success
1518  * \retval <0    failure
1519  */
1520 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
1521 {
1522         struct inode *inode = dentry->d_inode;
1523         struct obd_client_handle *och;
1524         int rc;
1525         ENTRY;
1526
1527         LASSERT(inode);
1528
1529         /* Root ? Do nothing. */
1530         if (dentry->d_inode->i_sb->s_root == dentry)
1531                 RETURN(0);
1532
1533         /* No open handle to close? Move away */
1534         if (!it_disposition(it, DISP_OPEN_OPEN))
1535                 RETURN(0);
1536
1537         LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
1538
1539         OBD_ALLOC(och, sizeof(*och));
1540         if (!och)
1541                 GOTO(out, rc = -ENOMEM);
1542
1543         ll_och_fill(ll_i2sbi(inode)->ll_md_exp,
1544                     ll_i2info(inode), it, och);
1545
1546         rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
1547                                        inode, och);
1548  out:
1549         /* this one is in place of ll_file_open */
1550         if (it_disposition(it, DISP_ENQ_OPEN_REF))
1551                 ptlrpc_req_finished(it->d.lustre.it_data);
1552         it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1553         RETURN(rc);
1554 }
1555
1556 /**
1557  * Get size for inode for which FIEMAP mapping is requested.
1558  * Make the FIEMAP get_info call and returns the result.
1559  */
1560 int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
1561               int num_bytes)
1562 {
1563         struct obd_export *exp = ll_i2dtexp(inode);
1564         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1565         struct ll_fiemap_info_key fm_key = { .name = KEY_FIEMAP, };
1566         int vallen = num_bytes;
1567         int rc;
1568         ENTRY;
1569
1570         /* Checks for fiemap flags */
1571         if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
1572                 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
1573                 return -EBADR;
1574         }
1575
1576         /* Check for FIEMAP_FLAG_SYNC */
1577         if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
1578                 rc = filemap_fdatawrite(inode->i_mapping);
1579                 if (rc)
1580                         return rc;
1581         }
1582
1583         /* If the stripe_count > 1 and the application does not understand
1584          * DEVICE_ORDER flag, then it cannot interpret the extents correctly.
1585          */
1586         if (lsm->lsm_stripe_count > 1 &&
1587             !(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER))
1588                 return -EOPNOTSUPP;
1589
1590         fm_key.oa.o_id = lsm->lsm_object_id;
1591         fm_key.oa.o_seq = lsm->lsm_object_seq;
1592         fm_key.oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1593
1594         obdo_from_inode(&fm_key.oa, inode, &ll_i2info(inode)->lli_fid,
1595                         OBD_MD_FLSIZE);
1596         /* If filesize is 0, then there would be no objects for mapping */
1597         if (fm_key.oa.o_size == 0) {
1598                 fiemap->fm_mapped_extents = 0;
1599                 RETURN(0);
1600         }
1601
1602         memcpy(&fm_key.fiemap, fiemap, sizeof(*fiemap));
1603
1604         rc = obd_get_info(exp, sizeof(fm_key), &fm_key, &vallen, fiemap, lsm);
1605         if (rc)
1606                 CERROR("obd_get_info failed: rc = %d\n", rc);
1607
1608         RETURN(rc);
1609 }
1610
1611 int ll_fid2path(struct obd_export *exp, void *arg)
1612 {
1613         struct getinfo_fid2path *gfout, *gfin;
1614         int outsize, rc;
1615         ENTRY;
1616
1617         /* Need to get the buflen */
1618         OBD_ALLOC_PTR(gfin);
1619         if (gfin == NULL)
1620                 RETURN(-ENOMEM);
1621         if (cfs_copy_from_user(gfin, arg, sizeof(*gfin))) {
1622                 OBD_FREE_PTR(gfin);
1623                 RETURN(-EFAULT);
1624         }
1625
1626         outsize = sizeof(*gfout) + gfin->gf_pathlen;
1627         OBD_ALLOC(gfout, outsize);
1628         if (gfout == NULL) {
1629                 OBD_FREE_PTR(gfin);
1630                 RETURN(-ENOMEM);
1631         }
1632         memcpy(gfout, gfin, sizeof(*gfout));
1633         OBD_FREE_PTR(gfin);
1634
1635         /* Call mdc_iocontrol */
1636         rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
1637         if (rc)
1638                 GOTO(gf_free, rc);
1639         if (cfs_copy_to_user(arg, gfout, outsize))
1640                 rc = -EFAULT;
1641
1642 gf_free:
1643         OBD_FREE(gfout, outsize);
1644         RETURN(rc);
1645 }
1646
1647 static int ll_ioctl_fiemap(struct inode *inode, unsigned long arg)
1648 {
1649         struct ll_user_fiemap *fiemap_s;
1650         size_t num_bytes, ret_bytes;
1651         unsigned int extent_count;
1652         int rc = 0;
1653
1654         /* Get the extent count so we can calculate the size of
1655          * required fiemap buffer */
1656         if (get_user(extent_count,
1657             &((struct ll_user_fiemap __user *)arg)->fm_extent_count))
1658                 RETURN(-EFAULT);
1659         num_bytes = sizeof(*fiemap_s) + (extent_count *
1660                                          sizeof(struct ll_fiemap_extent));
1661
1662         OBD_VMALLOC(fiemap_s, num_bytes);
1663         if (fiemap_s == NULL)
1664                 RETURN(-ENOMEM);
1665
1666         /* get the fiemap value */
1667         if (copy_from_user(fiemap_s,(struct ll_user_fiemap __user *)arg,
1668                            sizeof(*fiemap_s)))
1669                 GOTO(error, rc = -EFAULT);
1670
1671         /* If fm_extent_count is non-zero, read the first extent since
1672          * it is used to calculate end_offset and device from previous
1673          * fiemap call. */
1674         if (extent_count) {
1675                 if (copy_from_user(&fiemap_s->fm_extents[0],
1676                     (char __user *)arg + sizeof(*fiemap_s),
1677                     sizeof(struct ll_fiemap_extent)))
1678                         GOTO(error, rc = -EFAULT);
1679         }
1680
1681         rc = ll_do_fiemap(inode, fiemap_s, num_bytes);
1682         if (rc)
1683                 GOTO(error, rc);
1684
1685         ret_bytes = sizeof(struct ll_user_fiemap);
1686
1687         if (extent_count != 0)
1688                 ret_bytes += (fiemap_s->fm_mapped_extents *
1689                                  sizeof(struct ll_fiemap_extent));
1690
1691         if (copy_to_user((void *)arg, fiemap_s, ret_bytes))
1692                 rc = -EFAULT;
1693
1694 error:
1695         OBD_VFREE(fiemap_s, num_bytes);
1696         RETURN(rc);
1697 }
1698
1699 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1700                   unsigned long arg)
1701 {
1702         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1703         int flags;
1704         ENTRY;
1705
1706         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1707                inode->i_generation, inode, cmd);
1708         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
1709
1710         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1711         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1712                 RETURN(-ENOTTY);
1713
1714         switch(cmd) {
1715         case LL_IOC_GETFLAGS:
1716                 /* Get the current value of the file flags */
1717                 return put_user(fd->fd_flags, (int *)arg);
1718         case LL_IOC_SETFLAGS:
1719         case LL_IOC_CLRFLAGS:
1720                 /* Set or clear specific file flags */
1721                 /* XXX This probably needs checks to ensure the flags are
1722                  *     not abused, and to handle any flag side effects.
1723                  */
1724                 if (get_user(flags, (int *) arg))
1725                         RETURN(-EFAULT);
1726
1727                 if (cmd == LL_IOC_SETFLAGS) {
1728                         if ((flags & LL_FILE_IGNORE_LOCK) &&
1729                             !(file->f_flags & O_DIRECT)) {
1730                                 CERROR("%s: unable to disable locking on "
1731                                        "non-O_DIRECT file\n", current->comm);
1732                                 RETURN(-EINVAL);
1733                         }
1734
1735                         fd->fd_flags |= flags;
1736                 } else {
1737                         fd->fd_flags &= ~flags;
1738                 }
1739                 RETURN(0);
1740         case LL_IOC_LOV_SETSTRIPE:
1741                 RETURN(ll_lov_setstripe(inode, file, arg));
1742         case LL_IOC_LOV_SETEA:
1743                 RETURN(ll_lov_setea(inode, file, arg));
1744         case LL_IOC_LOV_GETSTRIPE:
1745                 RETURN(ll_lov_getstripe(inode, arg));
1746         case LL_IOC_RECREATE_OBJ:
1747                 RETURN(ll_lov_recreate_obj(inode, file, arg));
1748         case FSFILT_IOC_FIEMAP:
1749                 RETURN(ll_ioctl_fiemap(inode, arg));
1750         case FSFILT_IOC_GETFLAGS:
1751         case FSFILT_IOC_SETFLAGS:
1752                 RETURN(ll_iocontrol(inode, file, cmd, arg));
1753         case FSFILT_IOC_GETVERSION_OLD:
1754         case FSFILT_IOC_GETVERSION:
1755                 RETURN(put_user(inode->i_generation, (int *)arg));
1756         case LL_IOC_GROUP_LOCK:
1757                 RETURN(ll_get_grouplock(inode, file, arg));
1758         case LL_IOC_GROUP_UNLOCK:
1759                 RETURN(ll_put_grouplock(inode, file, arg));
1760         case IOC_OBD_STATFS:
1761                 RETURN(ll_obd_statfs(inode, (void *)arg));
1762
1763         /* We need to special case any other ioctls we want to handle,
1764          * to send them to the MDS/OST as appropriate and to properly
1765          * network encode the arg field.
1766         case FSFILT_IOC_SETVERSION_OLD:
1767         case FSFILT_IOC_SETVERSION:
1768         */
1769         case LL_IOC_FLUSHCTX:
1770                 RETURN(ll_flush_ctx(inode));
1771         case LL_IOC_PATH2FID: {
1772                 if (cfs_copy_to_user((void *)arg, ll_inode2fid(inode),
1773                                      sizeof(struct lu_fid)))
1774                         RETURN(-EFAULT);
1775
1776                 RETURN(0);
1777         }
1778         case OBD_IOC_FID2PATH:
1779                 RETURN(ll_fid2path(ll_i2mdexp(inode), (void *)arg));
1780
1781         case LL_IOC_GET_MDTIDX: {
1782                 int mdtidx;
1783
1784                 mdtidx = ll_get_mdt_idx(inode);
1785                 if (mdtidx < 0)
1786                         RETURN(mdtidx);
1787
1788                 if (put_user((int)mdtidx, (int*)arg))
1789                         RETURN(-EFAULT);
1790
1791                 RETURN(0);
1792         }
1793
1794         default: {
1795                 int err;
1796
1797                 if (LLIOC_STOP ==
1798                     ll_iocontrol_call(inode, file, cmd, arg, &err))
1799                         RETURN(err);
1800
1801                 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
1802                                      (void *)arg));
1803         }
1804         }
1805 }
1806
1807 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1808 {
1809         struct inode *inode = file->f_dentry->d_inode;
1810         loff_t retval;
1811         ENTRY;
1812         retval = offset + ((origin == 2) ? i_size_read(inode) :
1813                            (origin == 1) ? file->f_pos : 0);
1814         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), to=%Lu=%#Lx(%s)\n",
1815                inode->i_ino, inode->i_generation, inode, retval, retval,
1816                origin == 2 ? "SEEK_END": origin == 1 ? "SEEK_CUR" : "SEEK_SET");
1817         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
1818
1819         if (origin == 2) { /* SEEK_END */
1820                 int nonblock = 0, rc;
1821
1822                 if (file->f_flags & O_NONBLOCK)
1823                         nonblock = LDLM_FL_BLOCK_NOWAIT;
1824
1825                 rc = cl_glimpse_size(inode);
1826                 if (rc != 0)
1827                         RETURN(rc);
1828
1829                 offset += i_size_read(inode);
1830         } else if (origin == 1) { /* SEEK_CUR */
1831                 offset += file->f_pos;
1832         }
1833
1834         retval = -EINVAL;
1835         if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1836                 if (offset != file->f_pos) {
1837                         file->f_pos = offset;
1838                 }
1839                 retval = offset;
1840         }
1841
1842         RETURN(retval);
1843 }
1844
1845 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1846 {
1847         struct inode *inode = dentry->d_inode;
1848         struct ll_inode_info *lli = ll_i2info(inode);
1849         struct lov_stripe_md *lsm = lli->lli_smd;
1850         struct ptlrpc_request *req;
1851         struct obd_capa *oc;
1852         int rc, err;
1853         ENTRY;
1854         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1855                inode->i_generation, inode);
1856         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
1857
1858         /* fsync's caller has already called _fdata{sync,write}, we want
1859          * that IO to finish before calling the osc and mdc sync methods */
1860         rc = filemap_fdatawait(inode->i_mapping);
1861
1862         /* catch async errors that were recorded back when async writeback
1863          * failed for pages in this mapping. */
1864         err = lli->lli_async_rc;
1865         lli->lli_async_rc = 0;
1866         if (rc == 0)
1867                 rc = err;
1868         if (lsm) {
1869                 err = lov_test_and_clear_async_rc(lsm);
1870                 if (rc == 0)
1871                         rc = err;
1872         }
1873
1874         oc = ll_mdscapa_get(inode);
1875         err = md_sync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), oc,
1876                       &req);
1877         capa_put(oc);
1878         if (!rc)
1879                 rc = err;
1880         if (!err)
1881                 ptlrpc_req_finished(req);
1882
1883         if (data && lsm) {
1884                 struct obdo *oa;
1885
1886                 OBDO_ALLOC(oa);
1887                 if (!oa)
1888                         RETURN(rc ? rc : -ENOMEM);
1889
1890                 oa->o_id = lsm->lsm_object_id;
1891                 oa->o_seq = lsm->lsm_object_seq;
1892                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1893                 obdo_from_inode(oa, inode, &ll_i2info(inode)->lli_fid,
1894                                 OBD_MD_FLTYPE | OBD_MD_FLATIME |
1895                                 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1896                                 OBD_MD_FLGROUP);
1897
1898                 oc = ll_osscapa_get(inode, CAPA_OPC_OSS_WRITE);
1899                 err = obd_sync(ll_i2sbi(inode)->ll_dt_exp, oa, lsm,
1900                                0, OBD_OBJECT_EOF, oc);
1901                 capa_put(oc);
1902                 if (!rc)
1903                         rc = err;
1904                 OBDO_FREE(oa);
1905         }
1906
1907         RETURN(rc);
1908 }
1909
1910 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
1911 {
1912         struct inode *inode = file->f_dentry->d_inode;
1913         struct ll_sb_info *sbi = ll_i2sbi(inode);
1914         struct ldlm_enqueue_info einfo = { .ei_type = LDLM_FLOCK,
1915                                            .ei_cb_cp =ldlm_flock_completion_ast,
1916                                            .ei_cbdata = file_lock };
1917         struct md_op_data *op_data;
1918         struct lustre_handle lockh = {0};
1919         ldlm_policy_data_t flock;
1920         int flags = 0;
1921         int rc;
1922         ENTRY;
1923
1924         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
1925                inode->i_ino, file_lock);
1926
1927         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
1928
1929         if (file_lock->fl_flags & FL_FLOCK) {
1930                 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
1931                 /* set missing params for flock() calls */
1932                 file_lock->fl_end = OFFSET_MAX;
1933                 file_lock->fl_pid = current->tgid;
1934         }
1935         flock.l_flock.pid = file_lock->fl_pid;
1936         flock.l_flock.start = file_lock->fl_start;
1937         flock.l_flock.end = file_lock->fl_end;
1938
1939         switch (file_lock->fl_type) {
1940         case F_RDLCK:
1941                 einfo.ei_mode = LCK_PR;
1942                 break;
1943         case F_UNLCK:
1944                 /* An unlock request may or may not have any relation to
1945                  * existing locks so we may not be able to pass a lock handle
1946                  * via a normal ldlm_lock_cancel() request. The request may even
1947                  * unlock a byte range in the middle of an existing lock. In
1948                  * order to process an unlock request we need all of the same
1949                  * information that is given with a normal read or write record
1950                  * lock request. To avoid creating another ldlm unlock (cancel)
1951                  * message we'll treat a LCK_NL flock request as an unlock. */
1952                 einfo.ei_mode = LCK_NL;
1953                 break;
1954         case F_WRLCK:
1955                 einfo.ei_mode = LCK_PW;
1956                 break;
1957         default:
1958                 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1959                 RETURN (-EINVAL);
1960         }
1961
1962         switch (cmd) {
1963         case F_SETLKW:
1964 #ifdef F_SETLKW64
1965         case F_SETLKW64:
1966 #endif
1967                 flags = 0;
1968                 break;
1969         case F_SETLK:
1970 #ifdef F_SETLK64
1971         case F_SETLK64:
1972 #endif
1973                 flags = LDLM_FL_BLOCK_NOWAIT;
1974                 break;
1975         case F_GETLK:
1976 #ifdef F_GETLK64
1977         case F_GETLK64:
1978 #endif
1979                 flags = LDLM_FL_TEST_LOCK;
1980                 /* Save the old mode so that if the mode in the lock changes we
1981                  * can decrement the appropriate reader or writer refcount. */
1982                 file_lock->fl_type = einfo.ei_mode;
1983                 break;
1984         default:
1985                 CERROR("unknown fcntl lock command: %d\n", cmd);
1986                 RETURN (-EINVAL);
1987         }
1988
1989         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1990                                      LUSTRE_OPC_ANY, NULL);
1991         if (IS_ERR(op_data))
1992                 RETURN(PTR_ERR(op_data));
1993
1994         CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
1995                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
1996                flags, einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
1997
1998         rc = md_enqueue(sbi->ll_md_exp, &einfo, NULL,
1999                         op_data, &lockh, &flock, 0, NULL /* req */, flags);
2000
2001         ll_finish_md_op_data(op_data);
2002
2003         if ((file_lock->fl_flags & FL_FLOCK) &&
2004             (rc == 0 || file_lock->fl_type == F_UNLCK))
2005                 ll_flock_lock_file_wait(file, file_lock, (cmd == F_SETLKW));
2006 #ifdef HAVE_F_OP_FLOCK
2007         if ((file_lock->fl_flags & FL_POSIX) &&
2008             (rc == 0 || file_lock->fl_type == F_UNLCK) &&
2009             !(flags & LDLM_FL_TEST_LOCK))
2010                 posix_lock_file_wait(file, file_lock);
2011 #endif
2012
2013         RETURN(rc);
2014 }
2015
2016 int ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
2017 {
2018         ENTRY;
2019
2020         RETURN(-ENOSYS);
2021 }
2022
2023 int ll_have_md_lock(struct inode *inode, __u64 bits)
2024 {
2025         struct lustre_handle lockh;
2026         ldlm_policy_data_t policy = { .l_inodebits = {bits}};
2027         struct lu_fid *fid;
2028         int flags;
2029         ENTRY;
2030
2031         if (!inode)
2032                RETURN(0);
2033
2034         fid = &ll_i2info(inode)->lli_fid;
2035         CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
2036
2037         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
2038         if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
2039                           LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
2040                 RETURN(1);
2041         }
2042         RETURN(0);
2043 }
2044
2045 ldlm_mode_t ll_take_md_lock(struct inode *inode, __u64 bits,
2046                             struct lustre_handle *lockh)
2047 {
2048         ldlm_policy_data_t policy = { .l_inodebits = {bits}};
2049         struct lu_fid *fid;
2050         ldlm_mode_t rc;
2051         int flags;
2052         ENTRY;
2053
2054         fid = &ll_i2info(inode)->lli_fid;
2055         CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
2056
2057         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
2058         rc = md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
2059                            LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
2060         RETURN(rc);
2061 }
2062
2063 static int ll_inode_revalidate_fini(struct inode *inode, int rc) {
2064         if (rc == -ENOENT) { /* Already unlinked. Just update nlink
2065                               * and return success */
2066                 inode->i_nlink = 0;
2067                 /* This path cannot be hit for regular files unless in
2068                  * case of obscure races, so no need to to validate
2069                  * size. */
2070                 if (!S_ISREG(inode->i_mode) &&
2071                     !S_ISDIR(inode->i_mode))
2072                         return 0;
2073         }
2074
2075         if (rc) {
2076                 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2077                 return -abs(rc);
2078
2079         }
2080
2081         return 0;
2082 }
2083
2084 int __ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2085                              __u64 ibits)
2086 {
2087         struct inode *inode = dentry->d_inode;
2088         struct ptlrpc_request *req = NULL;
2089         struct ll_sb_info *sbi;
2090         struct obd_export *exp;
2091         int rc = 0;
2092         ENTRY;
2093
2094         if (!inode) {
2095                 CERROR("REPORT THIS LINE TO PETER\n");
2096                 RETURN(0);
2097         }
2098         sbi = ll_i2sbi(inode);
2099
2100         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
2101                inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
2102
2103         exp = ll_i2mdexp(inode);
2104
2105         if (exp->exp_connect_flags & OBD_CONNECT_ATTRFID) {
2106                 struct lookup_intent oit = { .it_op = IT_GETATTR };
2107                 struct md_op_data *op_data;
2108
2109                 /* Call getattr by fid, so do not provide name at all. */
2110                 op_data = ll_prep_md_op_data(NULL, dentry->d_parent->d_inode,
2111                                              dentry->d_inode, NULL, 0, 0,
2112                                              LUSTRE_OPC_ANY, NULL);
2113                 if (IS_ERR(op_data))
2114                         RETURN(PTR_ERR(op_data));
2115
2116                 oit.it_create_mode |= M_CHECK_STALE;
2117                 rc = md_intent_lock(exp, op_data, NULL, 0,
2118                                     /* we are not interested in name
2119                                        based lookup */
2120                                     &oit, 0, &req,
2121                                     ll_md_blocking_ast, 0);
2122                 ll_finish_md_op_data(op_data);
2123                 oit.it_create_mode &= ~M_CHECK_STALE;
2124                 if (rc < 0) {
2125                         rc = ll_inode_revalidate_fini(inode, rc);
2126                         GOTO (out, rc);
2127                 }
2128
2129                 rc = ll_revalidate_it_finish(req, &oit, dentry);
2130                 if (rc != 0) {
2131                         ll_intent_release(&oit);
2132                         GOTO(out, rc);
2133                 }
2134
2135                 /* Unlinked? Unhash dentry, so it is not picked up later by
2136                    do_lookup() -> ll_revalidate_it(). We cannot use d_drop
2137                    here to preserve get_cwd functionality on 2.6.
2138                    Bug 10503 */
2139                 if (!dentry->d_inode->i_nlink) {
2140                         cfs_spin_lock(&ll_lookup_lock);
2141                         spin_lock(&dcache_lock);
2142                         ll_drop_dentry(dentry);
2143                         spin_unlock(&dcache_lock);
2144                         cfs_spin_unlock(&ll_lookup_lock);
2145                 }
2146
2147                 ll_lookup_finish_locks(&oit, dentry);
2148         } else if (!ll_have_md_lock(dentry->d_inode, ibits)) {
2149                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
2150                 obd_valid valid = OBD_MD_FLGETATTR;
2151                 struct md_op_data *op_data;
2152                 int ealen = 0;
2153
2154                 if (S_ISREG(inode->i_mode)) {
2155                         rc = ll_get_max_mdsize(sbi, &ealen);
2156                         if (rc)
2157                                 RETURN(rc);
2158                         valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
2159                 }
2160
2161                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2162                                              0, ealen, LUSTRE_OPC_ANY,
2163                                              NULL);
2164                 if (op_data == NULL)
2165                         RETURN(-ENOMEM);
2166
2167                 op_data->op_valid = valid;
2168                 /* Once OBD_CONNECT_ATTRFID is not supported, we can't find one
2169                  * capa for this inode. Because we only keep capas of dirs
2170                  * fresh. */
2171                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2172                 ll_finish_md_op_data(op_data);
2173                 if (rc) {
2174                         rc = ll_inode_revalidate_fini(inode, rc);
2175                         RETURN(rc);
2176                 }
2177
2178                 rc = ll_prep_inode(&inode, req, NULL);
2179         }
2180 out:
2181         ptlrpc_req_finished(req);
2182         return rc;
2183 }
2184
2185 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
2186 {
2187         struct inode *inode = dentry->d_inode;
2188         int rc;
2189         ENTRY;
2190
2191         rc = __ll_inode_revalidate_it(dentry, it, MDS_INODELOCK_UPDATE |
2192                                                   MDS_INODELOCK_LOOKUP);
2193
2194         /* if object not yet allocated, don't validate size */
2195         if (rc == 0 && ll_i2info(dentry->d_inode)->lli_smd == NULL) {
2196                 LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_lvb.lvb_atime;
2197                 LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_lvb.lvb_mtime;
2198                 LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_lvb.lvb_ctime;
2199                 RETURN(0);
2200         }
2201
2202         /* cl_glimpse_size will prefer locally cached writes if they extend
2203          * the file */
2204
2205         if (rc == 0)
2206                 rc = cl_glimpse_size(inode);
2207
2208         RETURN(rc);
2209 }
2210
2211 int ll_getattr_it(struct vfsmount *mnt, struct dentry *de,
2212                   struct lookup_intent *it, struct kstat *stat)
2213 {
2214         struct inode *inode = de->d_inode;
2215         int res = 0;
2216
2217         res = ll_inode_revalidate_it(de, it);
2218         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETATTR, 1);
2219
2220         if (res)
2221                 return res;
2222
2223         stat->dev = inode->i_sb->s_dev;
2224         stat->ino = inode->i_ino;
2225         stat->mode = inode->i_mode;
2226         stat->nlink = inode->i_nlink;
2227         stat->uid = inode->i_uid;
2228         stat->gid = inode->i_gid;
2229         stat->rdev = kdev_t_to_nr(inode->i_rdev);
2230         stat->atime = inode->i_atime;
2231         stat->mtime = inode->i_mtime;
2232         stat->ctime = inode->i_ctime;
2233 #ifdef HAVE_INODE_BLKSIZE
2234         stat->blksize = inode->i_blksize;
2235 #else
2236         stat->blksize = 1 << inode->i_blkbits;
2237 #endif
2238
2239         stat->size = i_size_read(inode);
2240         stat->blocks = inode->i_blocks;
2241
2242         return 0;
2243 }
2244 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
2245 {
2246         struct lookup_intent it = { .it_op = IT_GETATTR };
2247
2248         return ll_getattr_it(mnt, de, &it, stat);
2249 }
2250
2251 #ifdef HAVE_LINUX_FIEMAP_H
2252 int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2253                 __u64 start, __u64 len)
2254 {
2255         int rc;
2256         size_t num_bytes;
2257         struct ll_user_fiemap *fiemap;
2258         unsigned int extent_count = fieinfo->fi_extents_max;
2259
2260         num_bytes = sizeof(*fiemap) + (extent_count *
2261                                        sizeof(struct ll_fiemap_extent));
2262         OBD_VMALLOC(fiemap, num_bytes);
2263
2264         if (fiemap == NULL)
2265                 RETURN(-ENOMEM);
2266
2267         fiemap->fm_flags = fieinfo->fi_flags;
2268         fiemap->fm_extent_count = fieinfo->fi_extents_max;
2269         fiemap->fm_start = start;
2270         fiemap->fm_length = len;
2271         memcpy(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
2272                sizeof(struct ll_fiemap_extent));
2273
2274         rc = ll_do_fiemap(inode, fiemap, num_bytes);
2275
2276         fieinfo->fi_flags = fiemap->fm_flags;
2277         fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
2278         memcpy(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
2279                fiemap->fm_mapped_extents * sizeof(struct ll_fiemap_extent));
2280
2281         OBD_VFREE(fiemap, num_bytes);
2282         return rc;
2283 }
2284 #endif
2285
2286
2287 static
2288 int lustre_check_acl(struct inode *inode, int mask)
2289 {
2290 #ifdef CONFIG_FS_POSIX_ACL
2291         struct ll_inode_info *lli = ll_i2info(inode);
2292         struct posix_acl *acl;
2293         int rc;
2294         ENTRY;
2295
2296         cfs_spin_lock(&lli->lli_lock);
2297         acl = posix_acl_dup(lli->lli_posix_acl);
2298         cfs_spin_unlock(&lli->lli_lock);
2299
2300         if (!acl)
2301                 RETURN(-EAGAIN);
2302
2303         rc = posix_acl_permission(inode, acl, mask);
2304         posix_acl_release(acl);
2305
2306         RETURN(rc);
2307 #else
2308         return -EAGAIN;
2309 #endif
2310 }
2311
2312 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
2313 #ifndef HAVE_INODE_PERMISION_2ARGS
2314 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2315 #else
2316 int ll_inode_permission(struct inode *inode, int mask)
2317 #endif
2318 {
2319         int rc = 0;
2320         ENTRY;
2321
2322        /* as root inode are NOT getting validated in lookup operation,
2323         * need to do it before permission check. */
2324
2325         if (inode == inode->i_sb->s_root->d_inode) {
2326                 struct lookup_intent it = { .it_op = IT_LOOKUP };
2327
2328                 rc = __ll_inode_revalidate_it(inode->i_sb->s_root, &it,
2329                                               MDS_INODELOCK_LOOKUP);
2330                 if (rc)
2331                         RETURN(rc);
2332         }
2333
2334         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), inode mode %x mask %o\n",
2335                inode->i_ino, inode->i_generation, inode, inode->i_mode, mask);
2336
2337         if (ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT)
2338                 return lustre_check_remote_perm(inode, mask);
2339
2340         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
2341         rc = generic_permission(inode, mask, lustre_check_acl);
2342
2343         RETURN(rc);
2344 }
2345 #else
2346 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2347 {
2348         int mode = inode->i_mode;
2349         int rc;
2350
2351         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), mask %o\n",
2352                inode->i_ino, inode->i_generation, inode, mask);
2353
2354         if (ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT)
2355                 return lustre_check_remote_perm(inode, mask);
2356
2357         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
2358
2359         if ((mask & MAY_WRITE) && IS_RDONLY(inode) &&
2360             (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2361                 return -EROFS;
2362         if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
2363                 return -EACCES;
2364         if (current->fsuid == inode->i_uid) {
2365                 mode >>= 6;
2366         } else if (1) {
2367                 if (((mode >> 3) & mask & S_IRWXO) != mask)
2368                         goto check_groups;
2369                 rc = lustre_check_acl(inode, mask);
2370                 if (rc == -EAGAIN)
2371                         goto check_groups;
2372                 if (rc == -EACCES)
2373                         goto check_capabilities;
2374                 return rc;
2375         } else {
2376 check_groups:
2377                 if (cfs_curproc_is_in_groups(inode->i_gid))
2378                         mode >>= 3;
2379         }
2380         if ((mode & mask & S_IRWXO) == mask)
2381                 return 0;
2382
2383 check_capabilities:
2384         if (!(mask & MAY_EXEC) ||
2385             (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
2386                 if (cfs_capable(CFS_CAP_DAC_OVERRIDE))
2387                         return 0;
2388
2389         if (cfs_capable(CFS_CAP_DAC_READ_SEARCH) && ((mask == MAY_READ) ||
2390             (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))))
2391                 return 0;
2392
2393         return -EACCES;
2394 }
2395 #endif
2396
2397 #ifdef HAVE_FILE_READV
2398 #define READ_METHOD readv
2399 #define READ_FUNCTION ll_file_readv
2400 #define WRITE_METHOD writev
2401 #define WRITE_FUNCTION ll_file_writev
2402 #else
2403 #define READ_METHOD aio_read
2404 #define READ_FUNCTION ll_file_aio_read
2405 #define WRITE_METHOD aio_write
2406 #define WRITE_FUNCTION ll_file_aio_write
2407 #endif
2408
2409 /* -o localflock - only provides locally consistent flock locks */
2410 struct file_operations ll_file_operations = {
2411         .read           = ll_file_read,
2412         .READ_METHOD    = READ_FUNCTION,
2413         .write          = ll_file_write,
2414         .WRITE_METHOD   = WRITE_FUNCTION,
2415         .ioctl          = ll_file_ioctl,
2416         .open           = ll_file_open,
2417         .release        = ll_file_release,
2418         .mmap           = ll_file_mmap,
2419         .llseek         = ll_file_seek,
2420 #ifdef HAVE_KERNEL_SENDFILE
2421         .sendfile       = ll_file_sendfile,
2422 #endif
2423 #ifdef HAVE_KERNEL_SPLICE_READ
2424         .splice_read    = ll_file_splice_read,
2425 #endif
2426         .fsync          = ll_fsync,
2427 };
2428
2429 struct file_operations ll_file_operations_flock = {
2430         .read           = ll_file_read,
2431         .READ_METHOD    = READ_FUNCTION,
2432         .write          = ll_file_write,
2433         .WRITE_METHOD   = WRITE_FUNCTION,
2434         .ioctl          = ll_file_ioctl,
2435         .open           = ll_file_open,
2436         .release        = ll_file_release,
2437         .mmap           = ll_file_mmap,
2438         .llseek         = ll_file_seek,
2439 #ifdef HAVE_KERNEL_SENDFILE
2440         .sendfile       = ll_file_sendfile,
2441 #endif
2442 #ifdef HAVE_KERNEL_SPLICE_READ
2443         .splice_read    = ll_file_splice_read,
2444 #endif
2445         .fsync          = ll_fsync,
2446 #ifdef HAVE_F_OP_FLOCK
2447         .flock          = ll_file_flock,
2448 #endif
2449         .lock           = ll_file_flock
2450 };
2451
2452 /* These are for -o noflock - to return ENOSYS on flock calls */
2453 struct file_operations ll_file_operations_noflock = {
2454         .read           = ll_file_read,
2455         .READ_METHOD    = READ_FUNCTION,
2456         .write          = ll_file_write,
2457         .WRITE_METHOD   = WRITE_FUNCTION,
2458         .ioctl          = ll_file_ioctl,
2459         .open           = ll_file_open,
2460         .release        = ll_file_release,
2461         .mmap           = ll_file_mmap,
2462         .llseek         = ll_file_seek,
2463 #ifdef HAVE_KERNEL_SENDFILE
2464         .sendfile       = ll_file_sendfile,
2465 #endif
2466 #ifdef HAVE_KERNEL_SPLICE_READ
2467         .splice_read    = ll_file_splice_read,
2468 #endif
2469         .fsync          = ll_fsync,
2470 #ifdef HAVE_F_OP_FLOCK
2471         .flock          = ll_file_noflock,
2472 #endif
2473         .lock           = ll_file_noflock
2474 };
2475
2476 struct inode_operations ll_file_inode_operations = {
2477 #ifdef HAVE_VFS_INTENT_PATCHES
2478         .setattr_raw    = ll_setattr_raw,
2479 #endif
2480         .setattr        = ll_setattr,
2481         .truncate       = ll_truncate,
2482         .getattr        = ll_getattr,
2483         .permission     = ll_inode_permission,
2484         .setxattr       = ll_setxattr,
2485         .getxattr       = ll_getxattr,
2486         .listxattr      = ll_listxattr,
2487         .removexattr    = ll_removexattr,
2488 #ifdef  HAVE_LINUX_FIEMAP_H
2489         .fiemap         = ll_fiemap,
2490 #endif
2491 };
2492
2493 /* dynamic ioctl number support routins */
2494 static struct llioc_ctl_data {
2495         cfs_rw_semaphore_t      ioc_sem;
2496         cfs_list_t              ioc_head;
2497 } llioc = {
2498         __RWSEM_INITIALIZER(llioc.ioc_sem),
2499         CFS_LIST_HEAD_INIT(llioc.ioc_head)
2500 };
2501
2502
2503 struct llioc_data {
2504         cfs_list_t              iocd_list;
2505         unsigned int            iocd_size;
2506         llioc_callback_t        iocd_cb;
2507         unsigned int            iocd_count;
2508         unsigned int            iocd_cmd[0];
2509 };
2510
2511 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd)
2512 {
2513         unsigned int size;
2514         struct llioc_data *in_data = NULL;
2515         ENTRY;
2516
2517         if (cb == NULL || cmd == NULL ||
2518             count > LLIOC_MAX_CMD || count < 0)
2519                 RETURN(NULL);
2520
2521         size = sizeof(*in_data) + count * sizeof(unsigned int);
2522         OBD_ALLOC(in_data, size);
2523         if (in_data == NULL)
2524                 RETURN(NULL);
2525
2526         memset(in_data, 0, sizeof(*in_data));
2527         in_data->iocd_size = size;
2528         in_data->iocd_cb = cb;
2529         in_data->iocd_count = count;
2530         memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count);
2531
2532         cfs_down_write(&llioc.ioc_sem);
2533         cfs_list_add_tail(&in_data->iocd_list, &llioc.ioc_head);
2534         cfs_up_write(&llioc.ioc_sem);
2535
2536         RETURN(in_data);
2537 }
2538
2539 void ll_iocontrol_unregister(void *magic)
2540 {
2541         struct llioc_data *tmp;
2542
2543         if (magic == NULL)
2544                 return;
2545
2546         cfs_down_write(&llioc.ioc_sem);
2547         cfs_list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) {
2548                 if (tmp == magic) {
2549                         unsigned int size = tmp->iocd_size;
2550
2551                         cfs_list_del(&tmp->iocd_list);
2552                         cfs_up_write(&llioc.ioc_sem);
2553
2554                         OBD_FREE(tmp, size);
2555                         return;
2556                 }
2557         }
2558         cfs_up_write(&llioc.ioc_sem);
2559
2560         CWARN("didn't find iocontrol register block with magic: %p\n", magic);
2561 }
2562
2563 EXPORT_SYMBOL(ll_iocontrol_register);
2564 EXPORT_SYMBOL(ll_iocontrol_unregister);
2565
2566 enum llioc_iter ll_iocontrol_call(struct inode *inode, struct file *file,
2567                         unsigned int cmd, unsigned long arg, int *rcp)
2568 {
2569         enum llioc_iter ret = LLIOC_CONT;
2570         struct llioc_data *data;
2571         int rc = -EINVAL, i;
2572
2573         cfs_down_read(&llioc.ioc_sem);
2574         cfs_list_for_each_entry(data, &llioc.ioc_head, iocd_list) {
2575                 for (i = 0; i < data->iocd_count; i++) {
2576                         if (cmd != data->iocd_cmd[i])
2577                                 continue;
2578
2579                         ret = data->iocd_cb(inode, file, cmd, arg, data, &rc);
2580                         break;
2581                 }
2582
2583                 if (ret == LLIOC_STOP)
2584                         break;
2585         }
2586         cfs_up_read(&llioc.ioc_sem);
2587
2588         if (rcp)
2589                 *rcp = rc;
2590         return ret;
2591 }