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