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