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