]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/llite/file.c
82248e9f6bffcbb70477d42c526cefb3e0dbc464
[~andy/linux] / drivers / staging / lustre / 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, Intel Corporation.
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, __GFP_IO);
58         if (fd == NULL)
59                 return NULL;
60         fd->fd_write_failed = false;
61         return fd;
62 }
63
64 static void ll_file_data_put(struct ll_file_data *fd)
65 {
66         if (fd != NULL)
67                 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
68 }
69
70 void ll_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
71                           struct lustre_handle *fh)
72 {
73         op_data->op_fid1 = ll_i2info(inode)->lli_fid;
74         op_data->op_attr.ia_mode = inode->i_mode;
75         op_data->op_attr.ia_atime = inode->i_atime;
76         op_data->op_attr.ia_mtime = inode->i_mtime;
77         op_data->op_attr.ia_ctime = inode->i_ctime;
78         op_data->op_attr.ia_size = i_size_read(inode);
79         op_data->op_attr_blocks = inode->i_blocks;
80         ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
81                                         ll_inode_to_ext_flags(inode->i_flags);
82         op_data->op_ioepoch = ll_i2info(inode)->lli_ioepoch;
83         if (fh)
84                 op_data->op_handle = *fh;
85         op_data->op_capa1 = ll_mdscapa_get(inode);
86
87         if (LLIF_DATA_MODIFIED & ll_i2info(inode)->lli_flags)
88                 op_data->op_bias |= MDS_DATA_MODIFIED;
89 }
90
91 /**
92  * Closes the IO epoch and packs all the attributes into @op_data for
93  * the CLOSE rpc.
94  */
95 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
96                              struct obd_client_handle *och)
97 {
98         op_data->op_attr.ia_valid = ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
99                                         ATTR_MTIME | ATTR_MTIME_SET |
100                                         ATTR_CTIME | ATTR_CTIME_SET;
101
102         if (!(och->och_flags & FMODE_WRITE))
103                 goto out;
104
105         if (!exp_connect_som(ll_i2mdexp(inode)) || !S_ISREG(inode->i_mode))
106                 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
107         else
108                 ll_ioepoch_close(inode, op_data, &och, 0);
109
110 out:
111         ll_pack_inode2opdata(inode, op_data, &och->och_fh);
112         ll_prep_md_op_data(op_data, inode, NULL, NULL,
113                            0, 0, LUSTRE_OPC_ANY, NULL);
114 }
115
116 static int ll_close_inode_openhandle(struct obd_export *md_exp,
117                                      struct inode *inode,
118                                      struct obd_client_handle *och)
119 {
120         struct obd_export *exp = ll_i2mdexp(inode);
121         struct md_op_data *op_data;
122         struct ptlrpc_request *req = NULL;
123         struct obd_device *obd = class_exp2obd(exp);
124         int epoch_close = 1;
125         int rc;
126
127         if (obd == NULL) {
128                 /*
129                  * XXX: in case of LMV, is this correct to access
130                  * ->exp_handle?
131                  */
132                 CERROR("Invalid MDC connection handle "LPX64"\n",
133                        ll_i2mdexp(inode)->exp_handle.h_cookie);
134                 GOTO(out, rc = 0);
135         }
136
137         OBD_ALLOC_PTR(op_data);
138         if (op_data == NULL)
139                 GOTO(out, rc = -ENOMEM); // XXX We leak openhandle and request here.
140
141         ll_prepare_close(inode, op_data, och);
142         epoch_close = (op_data->op_flags & MF_EPOCH_CLOSE);
143         rc = md_close(md_exp, op_data, och->och_mod, &req);
144         if (rc == -EAGAIN) {
145                 /* This close must have the epoch closed. */
146                 LASSERT(epoch_close);
147                 /* MDS has instructed us to obtain Size-on-MDS attribute from
148                  * OSTs and send setattr to back to MDS. */
149                 rc = ll_som_update(inode, op_data);
150                 if (rc) {
151                         CERROR("inode %lu mdc Size-on-MDS update failed: "
152                                "rc = %d\n", inode->i_ino, rc);
153                         rc = 0;
154                 }
155         } else if (rc) {
156                 CERROR("inode %lu mdc close failed: rc = %d\n",
157                        inode->i_ino, rc);
158         }
159
160         /* DATA_MODIFIED flag was successfully sent on close, cancel data
161          * modification flag. */
162         if (rc == 0 && (op_data->op_bias & MDS_DATA_MODIFIED)) {
163                 struct ll_inode_info *lli = ll_i2info(inode);
164
165                 spin_lock(&lli->lli_lock);
166                 lli->lli_flags &= ~LLIF_DATA_MODIFIED;
167                 spin_unlock(&lli->lli_lock);
168         }
169
170         ll_finish_md_op_data(op_data);
171
172         if (rc == 0) {
173                 rc = ll_objects_destroy(req, inode);
174                 if (rc)
175                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
176                                inode->i_ino, rc);
177         }
178
179 out:
180         if (exp_connect_som(exp) && !epoch_close &&
181             S_ISREG(inode->i_mode) && (och->och_flags & FMODE_WRITE)) {
182                 ll_queue_done_writing(inode, LLIF_DONE_WRITING);
183         } else {
184                 md_clear_open_replay_data(md_exp, och);
185                 /* Free @och if it is not waiting for DONE_WRITING. */
186                 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
187                 OBD_FREE_PTR(och);
188         }
189         if (req) /* This is close request */
190                 ptlrpc_req_finished(req);
191         return rc;
192 }
193
194 int ll_md_real_close(struct inode *inode, int flags)
195 {
196         struct ll_inode_info *lli = ll_i2info(inode);
197         struct obd_client_handle **och_p;
198         struct obd_client_handle *och;
199         __u64 *och_usecount;
200         int rc = 0;
201
202         if (flags & FMODE_WRITE) {
203                 och_p = &lli->lli_mds_write_och;
204                 och_usecount = &lli->lli_open_fd_write_count;
205         } else if (flags & FMODE_EXEC) {
206                 och_p = &lli->lli_mds_exec_och;
207                 och_usecount = &lli->lli_open_fd_exec_count;
208         } else {
209                 LASSERT(flags & FMODE_READ);
210                 och_p = &lli->lli_mds_read_och;
211                 och_usecount = &lli->lli_open_fd_read_count;
212         }
213
214         mutex_lock(&lli->lli_och_mutex);
215         if (*och_usecount) { /* There are still users of this handle, so
216                                 skip freeing it. */
217                 mutex_unlock(&lli->lli_och_mutex);
218                 return 0;
219         }
220         och=*och_p;
221         *och_p = NULL;
222         mutex_unlock(&lli->lli_och_mutex);
223
224         if (och) { /* There might be a race and somebody have freed this och
225                       already */
226                 rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
227                                                inode, och);
228         }
229
230         return rc;
231 }
232
233 int ll_md_close(struct obd_export *md_exp, struct inode *inode,
234                 struct file *file)
235 {
236         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
237         struct ll_inode_info *lli = ll_i2info(inode);
238         int rc = 0;
239
240         /* clear group lock, if present */
241         if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
242                 ll_put_grouplock(inode, file, fd->fd_grouplock.cg_gid);
243
244         if (fd->fd_lease_och != NULL) {
245                 bool lease_broken;
246
247                 /* Usually the lease is not released when the
248                  * application crashed, we need to release here. */
249                 rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken);
250                 CDEBUG(rc ? D_ERROR : D_INODE, "Clean up lease "DFID" %d/%d\n",
251                         PFID(&lli->lli_fid), rc, lease_broken);
252
253                 fd->fd_lease_och = NULL;
254         }
255
256         if (fd->fd_och != NULL) {
257                 rc = ll_close_inode_openhandle(md_exp, inode, fd->fd_och);
258                 fd->fd_och = NULL;
259                 GOTO(out, rc);
260         }
261
262         /* Let's see if we have good enough OPEN lock on the file and if
263            we can skip talking to MDS */
264         if (file->f_dentry->d_inode) { /* Can this ever be false? */
265                 int lockmode;
266                 int flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
267                 struct lustre_handle lockh;
268                 struct inode *inode = file->f_dentry->d_inode;
269                 ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_OPEN}};
270
271                 mutex_lock(&lli->lli_och_mutex);
272                 if (fd->fd_omode & FMODE_WRITE) {
273                         lockmode = LCK_CW;
274                         LASSERT(lli->lli_open_fd_write_count);
275                         lli->lli_open_fd_write_count--;
276                 } else if (fd->fd_omode & FMODE_EXEC) {
277                         lockmode = LCK_PR;
278                         LASSERT(lli->lli_open_fd_exec_count);
279                         lli->lli_open_fd_exec_count--;
280                 } else {
281                         lockmode = LCK_CR;
282                         LASSERT(lli->lli_open_fd_read_count);
283                         lli->lli_open_fd_read_count--;
284                 }
285                 mutex_unlock(&lli->lli_och_mutex);
286
287                 if (!md_lock_match(md_exp, flags, ll_inode2fid(inode),
288                                    LDLM_IBITS, &policy, lockmode,
289                                    &lockh)) {
290                         rc = ll_md_real_close(file->f_dentry->d_inode,
291                                               fd->fd_omode);
292                 }
293         } else {
294                 CERROR("Releasing a file %p with negative dentry %p. Name %s",
295                        file, file->f_dentry, file->f_dentry->d_name.name);
296         }
297
298 out:
299         LUSTRE_FPRIVATE(file) = NULL;
300         ll_file_data_put(fd);
301         ll_capa_close(inode);
302
303         return rc;
304 }
305
306 /* While this returns an error code, fput() the caller does not, so we need
307  * to make every effort to clean up all of our state here.  Also, applications
308  * rarely check close errors and even if an error is returned they will not
309  * re-try the close call.
310  */
311 int ll_file_release(struct inode *inode, struct file *file)
312 {
313         struct ll_file_data *fd;
314         struct ll_sb_info *sbi = ll_i2sbi(inode);
315         struct ll_inode_info *lli = ll_i2info(inode);
316         int rc;
317
318         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
319                inode->i_generation, inode);
320
321 #ifdef CONFIG_FS_POSIX_ACL
322         if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
323             inode == inode->i_sb->s_root->d_inode) {
324                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
325
326                 LASSERT(fd != NULL);
327                 if (unlikely(fd->fd_flags & LL_FILE_RMTACL)) {
328                         fd->fd_flags &= ~LL_FILE_RMTACL;
329                         rct_del(&sbi->ll_rct, current_pid());
330                         et_search_free(&sbi->ll_et, current_pid());
331                 }
332         }
333 #endif
334
335         if (inode->i_sb->s_root != file->f_dentry)
336                 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE, 1);
337         fd = LUSTRE_FPRIVATE(file);
338         LASSERT(fd != NULL);
339
340         /* The last ref on @file, maybe not the the owner pid of statahead.
341          * Different processes can open the same dir, "ll_opendir_key" means:
342          * it is me that should stop the statahead thread. */
343         if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd &&
344             lli->lli_opendir_pid != 0)
345                 ll_stop_statahead(inode, lli->lli_opendir_key);
346
347         if (inode->i_sb->s_root == file->f_dentry) {
348                 LUSTRE_FPRIVATE(file) = NULL;
349                 ll_file_data_put(fd);
350                 return 0;
351         }
352
353         if (!S_ISDIR(inode->i_mode)) {
354                 lov_read_and_clear_async_rc(lli->lli_clob);
355                 lli->lli_async_rc = 0;
356         }
357
358         rc = ll_md_close(sbi->ll_md_exp, inode, file);
359
360         if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
361                 libcfs_debug_dumplog();
362
363         return rc;
364 }
365
366 static int ll_intent_file_open(struct file *file, void *lmm,
367                                int lmmsize, struct lookup_intent *itp)
368 {
369         struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
370         struct dentry *parent = file->f_dentry->d_parent;
371         const char *name = file->f_dentry->d_name.name;
372         const int len = file->f_dentry->d_name.len;
373         struct md_op_data *op_data;
374         struct ptlrpc_request *req;
375         __u32 opc = LUSTRE_OPC_ANY;
376         int rc;
377
378         if (!parent)
379                 return -ENOENT;
380
381         /* Usually we come here only for NFSD, and we want open lock.
382            But we can also get here with pre 2.6.15 patchless kernels, and in
383            that case that lock is also ok */
384         /* We can also get here if there was cached open handle in revalidate_it
385          * but it disappeared while we were getting from there to ll_file_open.
386          * But this means this file was closed and immediately opened which
387          * makes a good candidate for using OPEN lock */
388         /* If lmmsize & lmm are not 0, we are just setting stripe info
389          * parameters. No need for the open lock */
390         if (lmm == NULL && lmmsize == 0) {
391                 itp->it_flags |= MDS_OPEN_LOCK;
392                 if (itp->it_flags & FMODE_WRITE)
393                         opc = LUSTRE_OPC_CREATE;
394         }
395
396         op_data  = ll_prep_md_op_data(NULL, parent->d_inode,
397                                       file->f_dentry->d_inode, name, len,
398                                       O_RDWR, opc, NULL);
399         if (IS_ERR(op_data))
400                 return PTR_ERR(op_data);
401
402         itp->it_flags |= MDS_OPEN_BY_FID;
403         rc = md_intent_lock(sbi->ll_md_exp, op_data, lmm, lmmsize, itp,
404                             0 /*unused */, &req, ll_md_blocking_ast, 0);
405         ll_finish_md_op_data(op_data);
406         if (rc == -ESTALE) {
407                 /* reason for keep own exit path - don`t flood log
408                 * with messages with -ESTALE errors.
409                 */
410                 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
411                      it_open_error(DISP_OPEN_OPEN, itp))
412                         GOTO(out, rc);
413                 ll_release_openhandle(file->f_dentry, itp);
414                 GOTO(out, rc);
415         }
416
417         if (it_disposition(itp, DISP_LOOKUP_NEG))
418                 GOTO(out, rc = -ENOENT);
419
420         if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
421                 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
422                 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
423                 GOTO(out, rc);
424         }
425
426         rc = ll_prep_inode(&file->f_dentry->d_inode, req, NULL, itp);
427         if (!rc && itp->d.lustre.it_lock_mode)
428                 ll_set_lock_data(sbi->ll_md_exp, file->f_dentry->d_inode,
429                                  itp, NULL);
430
431 out:
432         ptlrpc_req_finished(itp->d.lustre.it_data);
433         it_clear_disposition(itp, DISP_ENQ_COMPLETE);
434         ll_intent_drop_lock(itp);
435
436         return rc;
437 }
438
439 /**
440  * Assign an obtained @ioepoch to client's inode. No lock is needed, MDS does
441  * not believe attributes if a few ioepoch holders exist. Attributes for
442  * previous ioepoch if new one is opened are also skipped by MDS.
443  */
444 void ll_ioepoch_open(struct ll_inode_info *lli, __u64 ioepoch)
445 {
446         if (ioepoch && lli->lli_ioepoch != ioepoch) {
447                 lli->lli_ioepoch = ioepoch;
448                 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID"\n",
449                        ioepoch, PFID(&lli->lli_fid));
450         }
451 }
452
453 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
454                        struct obd_client_handle *och)
455 {
456         struct ptlrpc_request *req = it->d.lustre.it_data;
457         struct mdt_body *body;
458
459         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
460         och->och_fh = body->handle;
461         och->och_fid = body->fid1;
462         och->och_lease_handle.cookie = it->d.lustre.it_lock_handle;
463         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
464         och->och_flags = it->it_flags;
465
466         return md_set_open_replay_data(md_exp, och, req);
467 }
468
469 int ll_local_open(struct file *file, struct lookup_intent *it,
470                   struct ll_file_data *fd, struct obd_client_handle *och)
471 {
472         struct inode *inode = file->f_dentry->d_inode;
473         struct ll_inode_info *lli = ll_i2info(inode);
474
475         LASSERT(!LUSTRE_FPRIVATE(file));
476
477         LASSERT(fd != NULL);
478
479         if (och) {
480                 struct ptlrpc_request *req = it->d.lustre.it_data;
481                 struct mdt_body *body;
482                 int rc;
483
484                 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
485                 if (rc != 0)
486                         return rc;
487
488                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
489                 ll_ioepoch_open(lli, body->ioepoch);
490         }
491
492         LUSTRE_FPRIVATE(file) = fd;
493         ll_readahead_init(inode, &fd->fd_ras);
494         fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
495         return 0;
496 }
497
498 /* Open a file, and (for the very first open) create objects on the OSTs at
499  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
500  * creation or open until ll_lov_setstripe() ioctl is called.
501  *
502  * If we already have the stripe MD locally then we don't request it in
503  * md_open(), by passing a lmm_size = 0.
504  *
505  * It is up to the application to ensure no other processes open this file
506  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
507  * used.  We might be able to avoid races of that sort by getting lli_open_sem
508  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
509  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
510  */
511 int ll_file_open(struct inode *inode, struct file *file)
512 {
513         struct ll_inode_info *lli = ll_i2info(inode);
514         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
515                                           .it_flags = file->f_flags };
516         struct obd_client_handle **och_p = NULL;
517         __u64 *och_usecount = NULL;
518         struct ll_file_data *fd;
519         int rc = 0, opendir_set = 0;
520
521         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), flags %o\n", inode->i_ino,
522                inode->i_generation, inode, file->f_flags);
523
524         it = file->private_data; /* XXX: compat macro */
525         file->private_data = NULL; /* prevent ll_local_open assertion */
526
527         fd = ll_file_data_get();
528         if (fd == NULL)
529                 GOTO(out_openerr, rc = -ENOMEM);
530
531         fd->fd_file = file;
532         if (S_ISDIR(inode->i_mode)) {
533                 spin_lock(&lli->lli_sa_lock);
534                 if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL &&
535                     lli->lli_opendir_pid == 0) {
536                         lli->lli_opendir_key = fd;
537                         lli->lli_opendir_pid = current_pid();
538                         opendir_set = 1;
539                 }
540                 spin_unlock(&lli->lli_sa_lock);
541         }
542
543         if (inode->i_sb->s_root == file->f_dentry) {
544                 LUSTRE_FPRIVATE(file) = fd;
545                 return 0;
546         }
547
548         if (!it || !it->d.lustre.it_disposition) {
549                 /* Convert f_flags into access mode. We cannot use file->f_mode,
550                  * because everything but O_ACCMODE mask was stripped from
551                  * there */
552                 if ((oit.it_flags + 1) & O_ACCMODE)
553                         oit.it_flags++;
554                 if (file->f_flags & O_TRUNC)
555                         oit.it_flags |= FMODE_WRITE;
556
557                 /* kernel only call f_op->open in dentry_open.  filp_open calls
558                  * dentry_open after call to open_namei that checks permissions.
559                  * Only nfsd_open call dentry_open directly without checking
560                  * permissions and because of that this code below is safe. */
561                 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
562                         oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
563
564                 /* We do not want O_EXCL here, presumably we opened the file
565                  * already? XXX - NFS implications? */
566                 oit.it_flags &= ~O_EXCL;
567
568                 /* bug20584, if "it_flags" contains O_CREAT, the file will be
569                  * created if necessary, then "IT_CREAT" should be set to keep
570                  * consistent with it */
571                 if (oit.it_flags & O_CREAT)
572                         oit.it_op |= IT_CREAT;
573
574                 it = &oit;
575         }
576
577 restart:
578         /* Let's see if we have file open on MDS already. */
579         if (it->it_flags & FMODE_WRITE) {
580                 och_p = &lli->lli_mds_write_och;
581                 och_usecount = &lli->lli_open_fd_write_count;
582         } else if (it->it_flags & FMODE_EXEC) {
583                 och_p = &lli->lli_mds_exec_och;
584                 och_usecount = &lli->lli_open_fd_exec_count;
585          } else {
586                 och_p = &lli->lli_mds_read_och;
587                 och_usecount = &lli->lli_open_fd_read_count;
588         }
589
590         mutex_lock(&lli->lli_och_mutex);
591         if (*och_p) { /* Open handle is present */
592                 if (it_disposition(it, DISP_OPEN_OPEN)) {
593                         /* Well, there's extra open request that we do not need,
594                            let's close it somehow. This will decref request. */
595                         rc = it_open_error(DISP_OPEN_OPEN, it);
596                         if (rc) {
597                                 mutex_unlock(&lli->lli_och_mutex);
598                                 GOTO(out_openerr, rc);
599                         }
600
601                         ll_release_openhandle(file->f_dentry, it);
602                 }
603                 (*och_usecount)++;
604
605                 rc = ll_local_open(file, it, fd, NULL);
606                 if (rc) {
607                         (*och_usecount)--;
608                         mutex_unlock(&lli->lli_och_mutex);
609                         GOTO(out_openerr, rc);
610                 }
611         } else {
612                 LASSERT(*och_usecount == 0);
613                 if (!it->d.lustre.it_disposition) {
614                         /* We cannot just request lock handle now, new ELC code
615                            means that one of other OPEN locks for this file
616                            could be cancelled, and since blocking ast handler
617                            would attempt to grab och_mutex as well, that would
618                            result in a deadlock */
619                         mutex_unlock(&lli->lli_och_mutex);
620                         it->it_create_mode |= M_CHECK_STALE;
621                         rc = ll_intent_file_open(file, NULL, 0, it);
622                         it->it_create_mode &= ~M_CHECK_STALE;
623                         if (rc)
624                                 GOTO(out_openerr, rc);
625
626                         goto restart;
627                 }
628                 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
629                 if (!*och_p)
630                         GOTO(out_och_free, rc = -ENOMEM);
631
632                 (*och_usecount)++;
633
634                 /* md_intent_lock() didn't get a request ref if there was an
635                  * open error, so don't do cleanup on the request here
636                  * (bug 3430) */
637                 /* XXX (green): Should not we bail out on any error here, not
638                  * just open error? */
639                 rc = it_open_error(DISP_OPEN_OPEN, it);
640                 if (rc)
641                         GOTO(out_och_free, rc);
642
643                 LASSERT(it_disposition(it, DISP_ENQ_OPEN_REF));
644
645                 rc = ll_local_open(file, it, fd, *och_p);
646                 if (rc)
647                         GOTO(out_och_free, rc);
648         }
649         mutex_unlock(&lli->lli_och_mutex);
650         fd = NULL;
651
652         /* Must do this outside lli_och_mutex lock to prevent deadlock where
653            different kind of OPEN lock for this same inode gets cancelled
654            by ldlm_cancel_lru */
655         if (!S_ISREG(inode->i_mode))
656                 GOTO(out_och_free, rc);
657
658         ll_capa_open(inode);
659
660         if (!lli->lli_has_smd) {
661                 if (file->f_flags & O_LOV_DELAY_CREATE ||
662                     !(file->f_mode & FMODE_WRITE)) {
663                         CDEBUG(D_INODE, "object creation was delayed\n");
664                         GOTO(out_och_free, rc);
665                 }
666         }
667         file->f_flags &= ~O_LOV_DELAY_CREATE;
668         GOTO(out_och_free, rc);
669
670 out_och_free:
671         if (rc) {
672                 if (och_p && *och_p) {
673                         OBD_FREE(*och_p, sizeof (struct obd_client_handle));
674                         *och_p = NULL; /* OBD_FREE writes some magic there */
675                         (*och_usecount)--;
676                 }
677                 mutex_unlock(&lli->lli_och_mutex);
678
679 out_openerr:
680                 if (opendir_set != 0)
681                         ll_stop_statahead(inode, lli->lli_opendir_key);
682                 if (fd != NULL)
683                         ll_file_data_put(fd);
684         } else {
685                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN, 1);
686         }
687
688         if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
689                 ptlrpc_req_finished(it->d.lustre.it_data);
690                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
691         }
692
693         return rc;
694 }
695
696 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
697                         struct ldlm_lock_desc *desc, void *data, int flag)
698 {
699         int rc;
700         struct lustre_handle lockh;
701
702         switch (flag) {
703         case LDLM_CB_BLOCKING:
704                 ldlm_lock2handle(lock, &lockh);
705                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
706                 if (rc < 0) {
707                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
708                         return rc;
709                 }
710                 break;
711         case LDLM_CB_CANCELING:
712                 /* do nothing */
713                 break;
714         }
715         return 0;
716 }
717
718 /**
719  * Acquire a lease and open the file.
720  */
721 struct obd_client_handle *ll_lease_open(struct inode *inode, struct file *file,
722                                         fmode_t fmode)
723 {
724         struct lookup_intent it = { .it_op = IT_OPEN };
725         struct ll_sb_info *sbi = ll_i2sbi(inode);
726         struct md_op_data *op_data;
727         struct ptlrpc_request *req;
728         struct lustre_handle old_handle = { 0 };
729         struct obd_client_handle *och = NULL;
730         int rc;
731         int rc2;
732
733         if (fmode != FMODE_WRITE && fmode != FMODE_READ)
734                 return ERR_PTR(-EINVAL);
735
736         if (file != NULL) {
737                 struct ll_inode_info *lli = ll_i2info(inode);
738                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
739                 struct obd_client_handle **och_p;
740                 __u64 *och_usecount;
741
742                 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
743                         return ERR_PTR(-EPERM);
744
745                 /* Get the openhandle of the file */
746                 rc = -EBUSY;
747                 mutex_lock(&lli->lli_och_mutex);
748                 if (fd->fd_lease_och != NULL) {
749                         mutex_unlock(&lli->lli_och_mutex);
750                         return ERR_PTR(rc);
751                 }
752
753                 if (fd->fd_och == NULL) {
754                         if (file->f_mode & FMODE_WRITE) {
755                                 LASSERT(lli->lli_mds_write_och != NULL);
756                                 och_p = &lli->lli_mds_write_och;
757                                 och_usecount = &lli->lli_open_fd_write_count;
758                         } else {
759                                 LASSERT(lli->lli_mds_read_och != NULL);
760                                 och_p = &lli->lli_mds_read_och;
761                                 och_usecount = &lli->lli_open_fd_read_count;
762                         }
763                         if (*och_usecount == 1) {
764                                 fd->fd_och = *och_p;
765                                 *och_p = NULL;
766                                 *och_usecount = 0;
767                                 rc = 0;
768                         }
769                 }
770                 mutex_unlock(&lli->lli_och_mutex);
771                 if (rc < 0) /* more than 1 opener */
772                         return ERR_PTR(rc);
773
774                 LASSERT(fd->fd_och != NULL);
775                 old_handle = fd->fd_och->och_fh;
776         }
777
778         OBD_ALLOC_PTR(och);
779         if (och == NULL)
780                 return ERR_PTR(-ENOMEM);
781
782         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
783                                         LUSTRE_OPC_ANY, NULL);
784         if (IS_ERR(op_data))
785                 GOTO(out, rc = PTR_ERR(op_data));
786
787         /* To tell the MDT this openhandle is from the same owner */
788         op_data->op_handle = old_handle;
789
790         it.it_flags = fmode | MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
791         rc = md_intent_lock(sbi->ll_md_exp, op_data, NULL, 0, &it, 0, &req,
792                                 ll_md_blocking_lease_ast,
793         /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
794          * it can be cancelled which may mislead applications that the lease is
795          * broken;
796          * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
797          * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
798          * doesn't deal with openhandle, so normal openhandle will be leaked. */
799                                 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
800         ll_finish_md_op_data(op_data);
801         if (req != NULL) {
802                 ptlrpc_req_finished(req);
803                 it_clear_disposition(&it, DISP_ENQ_COMPLETE);
804         }
805         if (rc < 0)
806                 GOTO(out_release_it, rc);
807
808         if (it_disposition(&it, DISP_LOOKUP_NEG))
809                 GOTO(out_release_it, rc = -ENOENT);
810
811         rc = it_open_error(DISP_OPEN_OPEN, &it);
812         if (rc)
813                 GOTO(out_release_it, rc);
814
815         LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
816         ll_och_fill(sbi->ll_md_exp, &it, och);
817
818         if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
819                 GOTO(out_close, rc = -EOPNOTSUPP);
820
821         /* already get lease, handle lease lock */
822         ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
823         if (it.d.lustre.it_lock_mode == 0 ||
824             it.d.lustre.it_lock_bits != MDS_INODELOCK_OPEN) {
825                 /* open lock must return for lease */
826                 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
827                         PFID(ll_inode2fid(inode)), it.d.lustre.it_lock_mode,
828                         it.d.lustre.it_lock_bits);
829                 GOTO(out_close, rc = -EPROTO);
830         }
831
832         ll_intent_release(&it);
833         return och;
834
835 out_close:
836         rc2 = ll_close_inode_openhandle(sbi->ll_md_exp, inode, och);
837         if (rc2)
838                 CERROR("Close openhandle returned %d\n", rc2);
839
840         /* cancel open lock */
841         if (it.d.lustre.it_lock_mode != 0) {
842                 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
843                                                 it.d.lustre.it_lock_mode);
844                 it.d.lustre.it_lock_mode = 0;
845         }
846 out_release_it:
847         ll_intent_release(&it);
848 out:
849         OBD_FREE_PTR(och);
850         return ERR_PTR(rc);
851 }
852 EXPORT_SYMBOL(ll_lease_open);
853
854 /**
855  * Release lease and close the file.
856  * It will check if the lease has ever broken.
857  */
858 int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
859                         bool *lease_broken)
860 {
861         struct ldlm_lock *lock;
862         bool cancelled = true;
863         int rc;
864
865         lock = ldlm_handle2lock(&och->och_lease_handle);
866         if (lock != NULL) {
867                 lock_res_and_lock(lock);
868                 cancelled = ldlm_is_cancel(lock);
869                 unlock_res_and_lock(lock);
870                 ldlm_lock_put(lock);
871         }
872
873         CDEBUG(D_INODE, "lease for "DFID" broken? %d\n",
874                 PFID(&ll_i2info(inode)->lli_fid), cancelled);
875
876         if (!cancelled)
877                 ldlm_cli_cancel(&och->och_lease_handle, 0);
878         if (lease_broken != NULL)
879                 *lease_broken = cancelled;
880
881         rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp, inode, och);
882         return rc;
883 }
884 EXPORT_SYMBOL(ll_lease_close);
885
886 /* Fills the obdo with the attributes for the lsm */
887 static int ll_lsm_getattr(struct lov_stripe_md *lsm, struct obd_export *exp,
888                           struct obd_capa *capa, struct obdo *obdo,
889                           __u64 ioepoch, int sync)
890 {
891         struct ptlrpc_request_set *set;
892         struct obd_info     oinfo = { { { 0 } } };
893         int                     rc;
894
895         LASSERT(lsm != NULL);
896
897         oinfo.oi_md = lsm;
898         oinfo.oi_oa = obdo;
899         oinfo.oi_oa->o_oi = lsm->lsm_oi;
900         oinfo.oi_oa->o_mode = S_IFREG;
901         oinfo.oi_oa->o_ioepoch = ioepoch;
902         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
903                                OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
904                                OBD_MD_FLBLKSZ | OBD_MD_FLATIME |
905                                OBD_MD_FLMTIME | OBD_MD_FLCTIME |
906                                OBD_MD_FLGROUP | OBD_MD_FLEPOCH |
907                                OBD_MD_FLDATAVERSION;
908         oinfo.oi_capa = capa;
909         if (sync) {
910                 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
911                 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
912         }
913
914         set = ptlrpc_prep_set();
915         if (set == NULL) {
916                 CERROR("can't allocate ptlrpc set\n");
917                 rc = -ENOMEM;
918         } else {
919                 rc = obd_getattr_async(exp, &oinfo, set);
920                 if (rc == 0)
921                         rc = ptlrpc_set_wait(set);
922                 ptlrpc_set_destroy(set);
923         }
924         if (rc == 0)
925                 oinfo.oi_oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
926                                          OBD_MD_FLATIME | OBD_MD_FLMTIME |
927                                          OBD_MD_FLCTIME | OBD_MD_FLSIZE |
928                                          OBD_MD_FLDATAVERSION);
929         return rc;
930 }
931
932 /**
933   * Performs the getattr on the inode and updates its fields.
934   * If @sync != 0, perform the getattr under the server-side lock.
935   */
936 int ll_inode_getattr(struct inode *inode, struct obdo *obdo,
937                      __u64 ioepoch, int sync)
938 {
939         struct obd_capa      *capa = ll_mdscapa_get(inode);
940         struct lov_stripe_md *lsm;
941         int rc;
942
943         lsm = ccc_inode_lsm_get(inode);
944         rc = ll_lsm_getattr(lsm, ll_i2dtexp(inode),
945                             capa, obdo, ioepoch, sync);
946         capa_put(capa);
947         if (rc == 0) {
948                 struct ost_id *oi = lsm ? &lsm->lsm_oi : &obdo->o_oi;
949
950                 obdo_refresh_inode(inode, obdo, obdo->o_valid);
951                 CDEBUG(D_INODE, "objid "DOSTID" size %llu, blocks %llu,"
952                        " blksize %lu\n", POSTID(oi), i_size_read(inode),
953                        (unsigned long long)inode->i_blocks,
954                        (unsigned long)ll_inode_blksize(inode));
955         }
956         ccc_inode_lsm_put(inode, lsm);
957         return rc;
958 }
959
960 int ll_merge_lvb(const struct lu_env *env, struct inode *inode)
961 {
962         struct ll_inode_info *lli = ll_i2info(inode);
963         struct cl_object *obj = lli->lli_clob;
964         struct cl_attr *attr = ccc_env_thread_attr(env);
965         struct ost_lvb lvb;
966         int rc = 0;
967
968         ll_inode_size_lock(inode);
969         /* merge timestamps the most recently obtained from mds with
970            timestamps obtained from osts */
971         LTIME_S(inode->i_atime) = lli->lli_lvb.lvb_atime;
972         LTIME_S(inode->i_mtime) = lli->lli_lvb.lvb_mtime;
973         LTIME_S(inode->i_ctime) = lli->lli_lvb.lvb_ctime;
974         inode_init_lvb(inode, &lvb);
975
976         cl_object_attr_lock(obj);
977         rc = cl_object_attr_get(env, obj, attr);
978         cl_object_attr_unlock(obj);
979
980         if (rc == 0) {
981                 if (lvb.lvb_atime < attr->cat_atime)
982                         lvb.lvb_atime = attr->cat_atime;
983                 if (lvb.lvb_ctime < attr->cat_ctime)
984                         lvb.lvb_ctime = attr->cat_ctime;
985                 if (lvb.lvb_mtime < attr->cat_mtime)
986                         lvb.lvb_mtime = attr->cat_mtime;
987
988                 CDEBUG(D_VFSTRACE, DFID" updating i_size "LPU64"\n",
989                                 PFID(&lli->lli_fid), attr->cat_size);
990                 cl_isize_write_nolock(inode, attr->cat_size);
991
992                 inode->i_blocks = attr->cat_blocks;
993
994                 LTIME_S(inode->i_mtime) = lvb.lvb_mtime;
995                 LTIME_S(inode->i_atime) = lvb.lvb_atime;
996                 LTIME_S(inode->i_ctime) = lvb.lvb_ctime;
997         }
998         ll_inode_size_unlock(inode);
999
1000         return rc;
1001 }
1002
1003 int ll_glimpse_ioctl(struct ll_sb_info *sbi, struct lov_stripe_md *lsm,
1004                      lstat_t *st)
1005 {
1006         struct obdo obdo = { 0 };
1007         int rc;
1008
1009         rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, NULL, &obdo, 0, 0);
1010         if (rc == 0) {
1011                 st->st_size   = obdo.o_size;
1012                 st->st_blocks = obdo.o_blocks;
1013                 st->st_mtime  = obdo.o_mtime;
1014                 st->st_atime  = obdo.o_atime;
1015                 st->st_ctime  = obdo.o_ctime;
1016         }
1017         return rc;
1018 }
1019
1020 void ll_io_init(struct cl_io *io, const struct file *file, int write)
1021 {
1022         struct inode *inode = file->f_dentry->d_inode;
1023
1024         io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1025         if (write) {
1026                 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1027                 io->u.ci_wr.wr_sync = file->f_flags & O_SYNC ||
1028                                       file->f_flags & O_DIRECT ||
1029                                       IS_SYNC(inode);
1030         }
1031         io->ci_obj     = ll_i2info(inode)->lli_clob;
1032         io->ci_lockreq = CILR_MAYBE;
1033         if (ll_file_nolock(file)) {
1034                 io->ci_lockreq = CILR_NEVER;
1035                 io->ci_no_srvlock = 1;
1036         } else if (file->f_flags & O_APPEND) {
1037                 io->ci_lockreq = CILR_MANDATORY;
1038         }
1039 }
1040
1041 static ssize_t
1042 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1043                    struct file *file, enum cl_io_type iot,
1044                    loff_t *ppos, size_t count)
1045 {
1046         struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
1047         struct ll_file_data  *fd  = LUSTRE_FPRIVATE(file);
1048         struct cl_io     *io;
1049         ssize_t        result;
1050
1051 restart:
1052         io = ccc_env_thread_io(env);
1053         ll_io_init(io, file, iot == CIT_WRITE);
1054
1055         if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
1056                 struct vvp_io *vio = vvp_env_io(env);
1057                 struct ccc_io *cio = ccc_env_io(env);
1058                 int write_mutex_locked = 0;
1059
1060                 cio->cui_fd  = LUSTRE_FPRIVATE(file);
1061                 vio->cui_io_subtype = args->via_io_subtype;
1062
1063                 switch (vio->cui_io_subtype) {
1064                 case IO_NORMAL:
1065                         cio->cui_iov = args->u.normal.via_iov;
1066                         cio->cui_nrsegs = args->u.normal.via_nrsegs;
1067                         cio->cui_tot_nrsegs = cio->cui_nrsegs;
1068                         cio->cui_iocb = args->u.normal.via_iocb;
1069                         if ((iot == CIT_WRITE) &&
1070                             !(cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1071                                 if (mutex_lock_interruptible(&lli->
1072                                                                lli_write_mutex))
1073                                         GOTO(out, result = -ERESTARTSYS);
1074                                 write_mutex_locked = 1;
1075                         } else if (iot == CIT_READ) {
1076                                 down_read(&lli->lli_trunc_sem);
1077                         }
1078                         break;
1079                 case IO_SENDFILE:
1080                         vio->u.sendfile.cui_actor = args->u.sendfile.via_actor;
1081                         vio->u.sendfile.cui_target = args->u.sendfile.via_target;
1082                         break;
1083                 case IO_SPLICE:
1084                         vio->u.splice.cui_pipe = args->u.splice.via_pipe;
1085                         vio->u.splice.cui_flags = args->u.splice.via_flags;
1086                         break;
1087                 default:
1088                         CERROR("Unknow IO type - %u\n", vio->cui_io_subtype);
1089                         LBUG();
1090                 }
1091                 result = cl_io_loop(env, io);
1092                 if (write_mutex_locked)
1093                         mutex_unlock(&lli->lli_write_mutex);
1094                 else if (args->via_io_subtype == IO_NORMAL && iot == CIT_READ)
1095                         up_read(&lli->lli_trunc_sem);
1096         } else {
1097                 /* cl_io_rw_init() handled IO */
1098                 result = io->ci_result;
1099         }
1100
1101         if (io->ci_nob > 0) {
1102                 result = io->ci_nob;
1103                 *ppos = io->u.ci_wr.wr.crw_pos;
1104         }
1105         GOTO(out, result);
1106 out:
1107         cl_io_fini(env, io);
1108         /* If any bit been read/written (result != 0), we just return
1109          * short read/write instead of restart io. */
1110         if ((result == 0 || result == -ENODATA) && io->ci_need_restart) {
1111                 CDEBUG(D_VFSTRACE, "Restart %s on %s from %lld, count:%zd\n",
1112                        iot == CIT_READ ? "read" : "write",
1113                        file->f_dentry->d_name.name, *ppos, count);
1114                 LASSERTF(io->ci_nob == 0, "%zd", io->ci_nob);
1115                 goto restart;
1116         }
1117
1118         if (iot == CIT_READ) {
1119                 if (result >= 0)
1120                         ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode),
1121                                            LPROC_LL_READ_BYTES, result);
1122         } else if (iot == CIT_WRITE) {
1123                 if (result >= 0) {
1124                         ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode),
1125                                            LPROC_LL_WRITE_BYTES, result);
1126                         fd->fd_write_failed = false;
1127                 } else if (result != -ERESTARTSYS) {
1128                         fd->fd_write_failed = true;
1129                 }
1130         }
1131
1132         return result;
1133 }
1134
1135
1136 /*
1137  * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
1138  */
1139 static int ll_file_get_iov_count(const struct iovec *iov,
1140                                  unsigned long *nr_segs, size_t *count)
1141 {
1142         size_t cnt = 0;
1143         unsigned long seg;
1144
1145         for (seg = 0; seg < *nr_segs; seg++) {
1146                 const struct iovec *iv = &iov[seg];
1147
1148                 /*
1149                  * If any segment has a negative length, or the cumulative
1150                  * length ever wraps negative then return -EINVAL.
1151                  */
1152                 cnt += iv->iov_len;
1153                 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1154                         return -EINVAL;
1155                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
1156                         continue;
1157                 if (seg == 0)
1158                         return -EFAULT;
1159                 *nr_segs = seg;
1160                 cnt -= iv->iov_len;   /* This segment is no good */
1161                 break;
1162         }
1163         *count = cnt;
1164         return 0;
1165 }
1166
1167 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1168                                 unsigned long nr_segs, loff_t pos)
1169 {
1170         struct lu_env      *env;
1171         struct vvp_io_args *args;
1172         size_t        count;
1173         ssize_t      result;
1174         int              refcheck;
1175
1176         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1177         if (result)
1178                 return result;
1179
1180         env = cl_env_get(&refcheck);
1181         if (IS_ERR(env))
1182                 return PTR_ERR(env);
1183
1184         args = vvp_env_args(env, IO_NORMAL);
1185         args->u.normal.via_iov = (struct iovec *)iov;
1186         args->u.normal.via_nrsegs = nr_segs;
1187         args->u.normal.via_iocb = iocb;
1188
1189         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_READ,
1190                                     &iocb->ki_pos, count);
1191         cl_env_put(env, &refcheck);
1192         return result;
1193 }
1194
1195 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
1196                             loff_t *ppos)
1197 {
1198         struct lu_env *env;
1199         struct iovec  *local_iov;
1200         struct kiocb  *kiocb;
1201         ssize_t result;
1202         int         refcheck;
1203
1204         env = cl_env_get(&refcheck);
1205         if (IS_ERR(env))
1206                 return PTR_ERR(env);
1207
1208         local_iov = &vvp_env_info(env)->vti_local_iov;
1209         kiocb = &vvp_env_info(env)->vti_kiocb;
1210         local_iov->iov_base = (void __user *)buf;
1211         local_iov->iov_len = count;
1212         init_sync_kiocb(kiocb, file);
1213         kiocb->ki_pos = *ppos;
1214         kiocb->ki_nbytes = count;
1215
1216         result = ll_file_aio_read(kiocb, local_iov, 1, kiocb->ki_pos);
1217         *ppos = kiocb->ki_pos;
1218
1219         cl_env_put(env, &refcheck);
1220         return result;
1221 }
1222
1223 /*
1224  * Write to a file (through the page cache).
1225  */
1226 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1227                                  unsigned long nr_segs, loff_t pos)
1228 {
1229         struct lu_env      *env;
1230         struct vvp_io_args *args;
1231         size_t        count;
1232         ssize_t      result;
1233         int              refcheck;
1234
1235         result = ll_file_get_iov_count(iov, &nr_segs, &count);
1236         if (result)
1237                 return result;
1238
1239         env = cl_env_get(&refcheck);
1240         if (IS_ERR(env))
1241                 return PTR_ERR(env);
1242
1243         args = vvp_env_args(env, IO_NORMAL);
1244         args->u.normal.via_iov = (struct iovec *)iov;
1245         args->u.normal.via_nrsegs = nr_segs;
1246         args->u.normal.via_iocb = iocb;
1247
1248         result = ll_file_io_generic(env, args, iocb->ki_filp, CIT_WRITE,
1249                                   &iocb->ki_pos, count);
1250         cl_env_put(env, &refcheck);
1251         return result;
1252 }
1253
1254 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
1255                              loff_t *ppos)
1256 {
1257         struct lu_env *env;
1258         struct iovec  *local_iov;
1259         struct kiocb  *kiocb;
1260         ssize_t result;
1261         int         refcheck;
1262
1263         env = cl_env_get(&refcheck);
1264         if (IS_ERR(env))
1265                 return PTR_ERR(env);
1266
1267         local_iov = &vvp_env_info(env)->vti_local_iov;
1268         kiocb = &vvp_env_info(env)->vti_kiocb;
1269         local_iov->iov_base = (void __user *)buf;
1270         local_iov->iov_len = count;
1271         init_sync_kiocb(kiocb, file);
1272         kiocb->ki_pos = *ppos;
1273         kiocb->ki_nbytes = count;
1274
1275         result = ll_file_aio_write(kiocb, local_iov, 1, kiocb->ki_pos);
1276         *ppos = kiocb->ki_pos;
1277
1278         cl_env_put(env, &refcheck);
1279         return result;
1280 }
1281
1282
1283
1284 /*
1285  * Send file content (through pagecache) somewhere with helper
1286  */
1287 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
1288                                    struct pipe_inode_info *pipe, size_t count,
1289                                    unsigned int flags)
1290 {
1291         struct lu_env      *env;
1292         struct vvp_io_args *args;
1293         ssize_t      result;
1294         int              refcheck;
1295
1296         env = cl_env_get(&refcheck);
1297         if (IS_ERR(env))
1298                 return PTR_ERR(env);
1299
1300         args = vvp_env_args(env, IO_SPLICE);
1301         args->u.splice.via_pipe = pipe;
1302         args->u.splice.via_flags = flags;
1303
1304         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
1305         cl_env_put(env, &refcheck);
1306         return result;
1307 }
1308
1309 static int ll_lov_recreate(struct inode *inode, struct ost_id *oi,
1310                            obd_count ost_idx)
1311 {
1312         struct obd_export *exp = ll_i2dtexp(inode);
1313         struct obd_trans_info oti = { 0 };
1314         struct obdo *oa = NULL;
1315         int lsm_size;
1316         int rc = 0;
1317         struct lov_stripe_md *lsm = NULL, *lsm2;
1318
1319         OBDO_ALLOC(oa);
1320         if (oa == NULL)
1321                 return -ENOMEM;
1322
1323         lsm = ccc_inode_lsm_get(inode);
1324         if (!lsm_has_objects(lsm))
1325                 GOTO(out, rc = -ENOENT);
1326
1327         lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
1328                    (lsm->lsm_stripe_count));
1329
1330         OBD_ALLOC_LARGE(lsm2, lsm_size);
1331         if (lsm2 == NULL)
1332                 GOTO(out, rc = -ENOMEM);
1333
1334         oa->o_oi = *oi;
1335         oa->o_nlink = ost_idx;
1336         oa->o_flags |= OBD_FL_RECREATE_OBJS;
1337         oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1338         obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1339                                    OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1340         obdo_set_parent_fid(oa, &ll_i2info(inode)->lli_fid);
1341         memcpy(lsm2, lsm, lsm_size);
1342         ll_inode_size_lock(inode);
1343         rc = obd_create(NULL, exp, oa, &lsm2, &oti);
1344         ll_inode_size_unlock(inode);
1345
1346         OBD_FREE_LARGE(lsm2, lsm_size);
1347         GOTO(out, rc);
1348 out:
1349         ccc_inode_lsm_put(inode, lsm);
1350         OBDO_FREE(oa);
1351         return rc;
1352 }
1353
1354 static int ll_lov_recreate_obj(struct inode *inode, unsigned long arg)
1355 {
1356         struct ll_recreate_obj ucreat;
1357         struct ost_id           oi;
1358
1359         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1360                 return -EPERM;
1361
1362         if (copy_from_user(&ucreat, (struct ll_recreate_obj *)arg,
1363                            sizeof(ucreat)))
1364                 return -EFAULT;
1365
1366         ostid_set_seq_mdt0(&oi);
1367         ostid_set_id(&oi, ucreat.lrc_id);
1368         return ll_lov_recreate(inode, &oi, ucreat.lrc_ost_idx);
1369 }
1370
1371 static int ll_lov_recreate_fid(struct inode *inode, unsigned long arg)
1372 {
1373         struct lu_fid   fid;
1374         struct ost_id   oi;
1375         obd_count       ost_idx;
1376
1377         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1378                 return -EPERM;
1379
1380         if (copy_from_user(&fid, (struct lu_fid *)arg, sizeof(fid)))
1381                 return -EFAULT;
1382
1383         fid_to_ostid(&fid, &oi);
1384         ost_idx = (fid_seq(&fid) >> 16) & 0xffff;
1385         return ll_lov_recreate(inode, &oi, ost_idx);
1386 }
1387
1388 int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1389                              int flags, struct lov_user_md *lum, int lum_size)
1390 {
1391         struct lov_stripe_md *lsm = NULL;
1392         struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1393         int rc = 0;
1394
1395         lsm = ccc_inode_lsm_get(inode);
1396         if (lsm != NULL) {
1397                 ccc_inode_lsm_put(inode, lsm);
1398                 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1399                        inode->i_ino);
1400                 return -EEXIST;
1401         }
1402
1403         ll_inode_size_lock(inode);
1404         rc = ll_intent_file_open(file, lum, lum_size, &oit);
1405         if (rc)
1406                 GOTO(out, rc);
1407         rc = oit.d.lustre.it_status;
1408         if (rc < 0)
1409                 GOTO(out_req_free, rc);
1410
1411         ll_release_openhandle(file->f_dentry, &oit);
1412
1413  out:
1414         ll_inode_size_unlock(inode);
1415         ll_intent_release(&oit);
1416         ccc_inode_lsm_put(inode, lsm);
1417         return rc;
1418 out_req_free:
1419         ptlrpc_req_finished((struct ptlrpc_request *) oit.d.lustre.it_data);
1420         goto out;
1421 }
1422
1423 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
1424                              struct lov_mds_md **lmmp, int *lmm_size,
1425                              struct ptlrpc_request **request)
1426 {
1427         struct ll_sb_info *sbi = ll_i2sbi(inode);
1428         struct mdt_body  *body;
1429         struct lov_mds_md *lmm = NULL;
1430         struct ptlrpc_request *req = NULL;
1431         struct md_op_data *op_data;
1432         int rc, lmmsize;
1433
1434         rc = ll_get_max_mdsize(sbi, &lmmsize);
1435         if (rc)
1436                 return rc;
1437
1438         op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
1439                                      strlen(filename), lmmsize,
1440                                      LUSTRE_OPC_ANY, NULL);
1441         if (IS_ERR(op_data))
1442                 return PTR_ERR(op_data);
1443
1444         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
1445         rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
1446         ll_finish_md_op_data(op_data);
1447         if (rc < 0) {
1448                 CDEBUG(D_INFO, "md_getattr_name failed "
1449                        "on %s: rc %d\n", filename, rc);
1450                 GOTO(out, rc);
1451         }
1452
1453         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1454         LASSERT(body != NULL); /* checked by mdc_getattr_name */
1455
1456         lmmsize = body->eadatasize;
1457
1458         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
1459                         lmmsize == 0) {
1460                 GOTO(out, rc = -ENODATA);
1461         }
1462
1463         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
1464         LASSERT(lmm != NULL);
1465
1466         if ((lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1)) &&
1467             (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3))) {
1468                 GOTO(out, rc = -EPROTO);
1469         }
1470
1471         /*
1472          * This is coming from the MDS, so is probably in
1473          * little endian.  We convert it to host endian before
1474          * passing it to userspace.
1475          */
1476         if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC)) {
1477                 int stripe_count;
1478
1479                 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
1480                 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
1481                         stripe_count = 0;
1482
1483                 /* if function called for directory - we should
1484                  * avoid swab not existent lsm objects */
1485                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1)) {
1486                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1487                         if (S_ISREG(body->mode))
1488                                 lustre_swab_lov_user_md_objects(
1489                                  ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1490                                  stripe_count);
1491                 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
1492                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1493                         if (S_ISREG(body->mode))
1494                                 lustre_swab_lov_user_md_objects(
1495                                  ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1496                                  stripe_count);
1497                 }
1498         }
1499
1500 out:
1501         *lmmp = lmm;
1502         *lmm_size = lmmsize;
1503         *request = req;
1504         return rc;
1505 }
1506
1507 static int ll_lov_setea(struct inode *inode, struct file *file,
1508                             unsigned long arg)
1509 {
1510         int                      flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1511         struct lov_user_md      *lump;
1512         int                      lum_size = sizeof(struct lov_user_md) +
1513                                             sizeof(struct lov_user_ost_data);
1514         int                      rc;
1515
1516         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1517                 return -EPERM;
1518
1519         OBD_ALLOC_LARGE(lump, lum_size);
1520         if (lump == NULL)
1521                 return -ENOMEM;
1522
1523         if (copy_from_user(lump, (struct lov_user_md  *)arg, lum_size)) {
1524                 OBD_FREE_LARGE(lump, lum_size);
1525                 return -EFAULT;
1526         }
1527
1528         rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1529
1530         OBD_FREE_LARGE(lump, lum_size);
1531         return rc;
1532 }
1533
1534 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1535                             unsigned long arg)
1536 {
1537         struct lov_user_md_v3    lumv3;
1538         struct lov_user_md_v1   *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1539         struct lov_user_md_v1   *lumv1p = (struct lov_user_md_v1 *)arg;
1540         struct lov_user_md_v3   *lumv3p = (struct lov_user_md_v3 *)arg;
1541         int                      lum_size, rc;
1542         int                      flags = FMODE_WRITE;
1543
1544         /* first try with v1 which is smaller than v3 */
1545         lum_size = sizeof(struct lov_user_md_v1);
1546         if (copy_from_user(lumv1, lumv1p, lum_size))
1547                 return -EFAULT;
1548
1549         if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
1550                 lum_size = sizeof(struct lov_user_md_v3);
1551                 if (copy_from_user(&lumv3, lumv3p, lum_size))
1552                         return -EFAULT;
1553         }
1554
1555         rc = ll_lov_setstripe_ea_info(inode, file, flags, lumv1, lum_size);
1556         if (rc == 0) {
1557                 struct lov_stripe_md *lsm;
1558                 __u32 gen;
1559
1560                 put_user(0, &lumv1p->lmm_stripe_count);
1561
1562                 ll_layout_refresh(inode, &gen);
1563                 lsm = ccc_inode_lsm_get(inode);
1564                 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1565                                    0, lsm, (void *)arg);
1566                 ccc_inode_lsm_put(inode, lsm);
1567         }
1568         return rc;
1569 }
1570
1571 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1572 {
1573         struct lov_stripe_md *lsm;
1574         int rc = -ENODATA;
1575
1576         lsm = ccc_inode_lsm_get(inode);
1577         if (lsm != NULL)
1578                 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0,
1579                                    lsm, (void *)arg);
1580         ccc_inode_lsm_put(inode, lsm);
1581         return rc;
1582 }
1583
1584 int ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1585 {
1586         struct ll_inode_info   *lli = ll_i2info(inode);
1587         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1588         struct ccc_grouplock    grouplock;
1589         int                  rc;
1590
1591         if (ll_file_nolock(file))
1592                 return -EOPNOTSUPP;
1593
1594         spin_lock(&lli->lli_lock);
1595         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1596                 CWARN("group lock already existed with gid %lu\n",
1597                       fd->fd_grouplock.cg_gid);
1598                 spin_unlock(&lli->lli_lock);
1599                 return -EINVAL;
1600         }
1601         LASSERT(fd->fd_grouplock.cg_lock == NULL);
1602         spin_unlock(&lli->lli_lock);
1603
1604         rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1605                               arg, (file->f_flags & O_NONBLOCK), &grouplock);
1606         if (rc)
1607                 return rc;
1608
1609         spin_lock(&lli->lli_lock);
1610         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1611                 spin_unlock(&lli->lli_lock);
1612                 CERROR("another thread just won the race\n");
1613                 cl_put_grouplock(&grouplock);
1614                 return -EINVAL;
1615         }
1616
1617         fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1618         fd->fd_grouplock = grouplock;
1619         spin_unlock(&lli->lli_lock);
1620
1621         CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
1622         return 0;
1623 }
1624
1625 int ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg)
1626 {
1627         struct ll_inode_info   *lli = ll_i2info(inode);
1628         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
1629         struct ccc_grouplock    grouplock;
1630
1631         spin_lock(&lli->lli_lock);
1632         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1633                 spin_unlock(&lli->lli_lock);
1634                 CWARN("no group lock held\n");
1635                 return -EINVAL;
1636         }
1637         LASSERT(fd->fd_grouplock.cg_lock != NULL);
1638
1639         if (fd->fd_grouplock.cg_gid != arg) {
1640                 CWARN("group lock %lu doesn't match current id %lu\n",
1641                        arg, fd->fd_grouplock.cg_gid);
1642                 spin_unlock(&lli->lli_lock);
1643                 return -EINVAL;
1644         }
1645
1646         grouplock = fd->fd_grouplock;
1647         memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1648         fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1649         spin_unlock(&lli->lli_lock);
1650
1651         cl_put_grouplock(&grouplock);
1652         CDEBUG(D_INFO, "group lock %lu released\n", arg);
1653         return 0;
1654 }
1655
1656 /**
1657  * Close inode open handle
1658  *
1659  * \param dentry [in]     dentry which contains the inode
1660  * \param it     [in,out] intent which contains open info and result
1661  *
1662  * \retval 0     success
1663  * \retval <0    failure
1664  */
1665 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
1666 {
1667         struct inode *inode = dentry->d_inode;
1668         struct obd_client_handle *och;
1669         int rc;
1670
1671         LASSERT(inode);
1672
1673         /* Root ? Do nothing. */
1674         if (dentry->d_inode->i_sb->s_root == dentry)
1675                 return 0;
1676
1677         /* No open handle to close? Move away */
1678         if (!it_disposition(it, DISP_OPEN_OPEN))
1679                 return 0;
1680
1681         LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
1682
1683         OBD_ALLOC(och, sizeof(*och));
1684         if (!och)
1685                 GOTO(out, rc = -ENOMEM);
1686
1687         ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
1688
1689         rc = ll_close_inode_openhandle(ll_i2sbi(inode)->ll_md_exp,
1690                                        inode, och);
1691  out:
1692         /* this one is in place of ll_file_open */
1693         if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
1694                 ptlrpc_req_finished(it->d.lustre.it_data);
1695                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1696         }
1697         return rc;
1698 }
1699
1700 /**
1701  * Get size for inode for which FIEMAP mapping is requested.
1702  * Make the FIEMAP get_info call and returns the result.
1703  */
1704 int ll_do_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
1705               int num_bytes)
1706 {
1707         struct obd_export *exp = ll_i2dtexp(inode);
1708         struct lov_stripe_md *lsm = NULL;
1709         struct ll_fiemap_info_key fm_key = { .name = KEY_FIEMAP, };
1710         int vallen = num_bytes;
1711         int rc;
1712
1713         /* Checks for fiemap flags */
1714         if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
1715                 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
1716                 return -EBADR;
1717         }
1718
1719         /* Check for FIEMAP_FLAG_SYNC */
1720         if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
1721                 rc = filemap_fdatawrite(inode->i_mapping);
1722                 if (rc)
1723                         return rc;
1724         }
1725
1726         lsm = ccc_inode_lsm_get(inode);
1727         if (lsm == NULL)
1728                 return -ENOENT;
1729
1730         /* If the stripe_count > 1 and the application does not understand
1731          * DEVICE_ORDER flag, then it cannot interpret the extents correctly.
1732          */
1733         if (lsm->lsm_stripe_count > 1 &&
1734             !(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER))
1735                 GOTO(out, rc = -EOPNOTSUPP);
1736
1737         fm_key.oa.o_oi = lsm->lsm_oi;
1738         fm_key.oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1739
1740         obdo_from_inode(&fm_key.oa, inode, OBD_MD_FLSIZE);
1741         obdo_set_parent_fid(&fm_key.oa, &ll_i2info(inode)->lli_fid);
1742         /* If filesize is 0, then there would be no objects for mapping */
1743         if (fm_key.oa.o_size == 0) {
1744                 fiemap->fm_mapped_extents = 0;
1745                 GOTO(out, rc = 0);
1746         }
1747
1748         memcpy(&fm_key.fiemap, fiemap, sizeof(*fiemap));
1749
1750         rc = obd_get_info(NULL, exp, sizeof(fm_key), &fm_key, &vallen,
1751                           fiemap, lsm);
1752         if (rc)
1753                 CERROR("obd_get_info failed: rc = %d\n", rc);
1754
1755 out:
1756         ccc_inode_lsm_put(inode, lsm);
1757         return rc;
1758 }
1759
1760 int ll_fid2path(struct inode *inode, void *arg)
1761 {
1762         struct obd_export       *exp = ll_i2mdexp(inode);
1763         struct getinfo_fid2path *gfout, *gfin;
1764         int                      outsize, rc;
1765
1766         if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
1767             !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
1768                 return -EPERM;
1769
1770         /* Need to get the buflen */
1771         OBD_ALLOC_PTR(gfin);
1772         if (gfin == NULL)
1773                 return -ENOMEM;
1774         if (copy_from_user(gfin, arg, sizeof(*gfin))) {
1775                 OBD_FREE_PTR(gfin);
1776                 return -EFAULT;
1777         }
1778
1779         outsize = sizeof(*gfout) + gfin->gf_pathlen;
1780         OBD_ALLOC(gfout, outsize);
1781         if (gfout == NULL) {
1782                 OBD_FREE_PTR(gfin);
1783                 return -ENOMEM;
1784         }
1785         memcpy(gfout, gfin, sizeof(*gfout));
1786         OBD_FREE_PTR(gfin);
1787
1788         /* Call mdc_iocontrol */
1789         rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
1790         if (rc)
1791                 GOTO(gf_free, rc);
1792
1793         if (copy_to_user(arg, gfout, outsize))
1794                 rc = -EFAULT;
1795
1796 gf_free:
1797         OBD_FREE(gfout, outsize);
1798         return rc;
1799 }
1800
1801 static int ll_ioctl_fiemap(struct inode *inode, unsigned long arg)
1802 {
1803         struct ll_user_fiemap *fiemap_s;
1804         size_t num_bytes, ret_bytes;
1805         unsigned int extent_count;
1806         int rc = 0;
1807
1808         /* Get the extent count so we can calculate the size of
1809          * required fiemap buffer */
1810         if (get_user(extent_count,
1811             &((struct ll_user_fiemap __user *)arg)->fm_extent_count))
1812                 return -EFAULT;
1813         num_bytes = sizeof(*fiemap_s) + (extent_count *
1814                                          sizeof(struct ll_fiemap_extent));
1815
1816         OBD_ALLOC_LARGE(fiemap_s, num_bytes);
1817         if (fiemap_s == NULL)
1818                 return -ENOMEM;
1819
1820         /* get the fiemap value */
1821         if (copy_from_user(fiemap_s, (struct ll_user_fiemap __user *)arg,
1822                            sizeof(*fiemap_s)))
1823                 GOTO(error, rc = -EFAULT);
1824
1825         /* If fm_extent_count is non-zero, read the first extent since
1826          * it is used to calculate end_offset and device from previous
1827          * fiemap call. */
1828         if (extent_count) {
1829                 if (copy_from_user(&fiemap_s->fm_extents[0],
1830                     (char __user *)arg + sizeof(*fiemap_s),
1831                     sizeof(struct ll_fiemap_extent)))
1832                         GOTO(error, rc = -EFAULT);
1833         }
1834
1835         rc = ll_do_fiemap(inode, fiemap_s, num_bytes);
1836         if (rc)
1837                 GOTO(error, rc);
1838
1839         ret_bytes = sizeof(struct ll_user_fiemap);
1840
1841         if (extent_count != 0)
1842                 ret_bytes += (fiemap_s->fm_mapped_extents *
1843                                  sizeof(struct ll_fiemap_extent));
1844
1845         if (copy_to_user((void *)arg, fiemap_s, ret_bytes))
1846                 rc = -EFAULT;
1847
1848 error:
1849         OBD_FREE_LARGE(fiemap_s, num_bytes);
1850         return rc;
1851 }
1852
1853 /*
1854  * Read the data_version for inode.
1855  *
1856  * This value is computed using stripe object version on OST.
1857  * Version is computed using server side locking.
1858  *
1859  * @param extent_lock  Take extent lock. Not needed if a process is already
1860  *                     holding the OST object group locks.
1861  */
1862 int ll_data_version(struct inode *inode, __u64 *data_version,
1863                     int extent_lock)
1864 {
1865         struct lov_stripe_md    *lsm = NULL;
1866         struct ll_sb_info       *sbi = ll_i2sbi(inode);
1867         struct obdo             *obdo = NULL;
1868         int                      rc;
1869
1870         /* If no stripe, we consider version is 0. */
1871         lsm = ccc_inode_lsm_get(inode);
1872         if (!lsm_has_objects(lsm)) {
1873                 *data_version = 0;
1874                 CDEBUG(D_INODE, "No object for inode\n");
1875                 GOTO(out, rc = 0);
1876         }
1877
1878         OBD_ALLOC_PTR(obdo);
1879         if (obdo == NULL)
1880                 GOTO(out, rc = -ENOMEM);
1881
1882         rc = ll_lsm_getattr(lsm, sbi->ll_dt_exp, NULL, obdo, 0, extent_lock);
1883         if (rc == 0) {
1884                 if (!(obdo->o_valid & OBD_MD_FLDATAVERSION))
1885                         rc = -EOPNOTSUPP;
1886                 else
1887                         *data_version = obdo->o_data_version;
1888         }
1889
1890         OBD_FREE_PTR(obdo);
1891 out:
1892         ccc_inode_lsm_put(inode, lsm);
1893         return rc;
1894 }
1895
1896 struct ll_swap_stack {
1897         struct iattr             ia1, ia2;
1898         __u64                    dv1, dv2;
1899         struct inode            *inode1, *inode2;
1900         bool                     check_dv1, check_dv2;
1901 };
1902
1903 static int ll_swap_layouts(struct file *file1, struct file *file2,
1904                            struct lustre_swap_layouts *lsl)
1905 {
1906         struct mdc_swap_layouts  msl;
1907         struct md_op_data       *op_data;
1908         __u32                    gid;
1909         __u64                    dv;
1910         struct ll_swap_stack    *llss = NULL;
1911         int                      rc;
1912
1913         OBD_ALLOC_PTR(llss);
1914         if (llss == NULL)
1915                 return -ENOMEM;
1916
1917         llss->inode1 = file1->f_dentry->d_inode;
1918         llss->inode2 = file2->f_dentry->d_inode;
1919
1920         if (!S_ISREG(llss->inode2->i_mode))
1921                 GOTO(free, rc = -EINVAL);
1922
1923         if (inode_permission(llss->inode1, MAY_WRITE) ||
1924             inode_permission(llss->inode2, MAY_WRITE))
1925                 GOTO(free, rc = -EPERM);
1926
1927         if (llss->inode2->i_sb != llss->inode1->i_sb)
1928                 GOTO(free, rc = -EXDEV);
1929
1930         /* we use 2 bool because it is easier to swap than 2 bits */
1931         if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
1932                 llss->check_dv1 = true;
1933
1934         if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
1935                 llss->check_dv2 = true;
1936
1937         /* we cannot use lsl->sl_dvX directly because we may swap them */
1938         llss->dv1 = lsl->sl_dv1;
1939         llss->dv2 = lsl->sl_dv2;
1940
1941         rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
1942         if (rc == 0) /* same file, done! */
1943                 GOTO(free, rc = 0);
1944
1945         if (rc < 0) { /* sequentialize it */
1946                 swap(llss->inode1, llss->inode2);
1947                 swap(file1, file2);
1948                 swap(llss->dv1, llss->dv2);
1949                 swap(llss->check_dv1, llss->check_dv2);
1950         }
1951
1952         gid = lsl->sl_gid;
1953         if (gid != 0) { /* application asks to flush dirty cache */
1954                 rc = ll_get_grouplock(llss->inode1, file1, gid);
1955                 if (rc < 0)
1956                         GOTO(free, rc);
1957
1958                 rc = ll_get_grouplock(llss->inode2, file2, gid);
1959                 if (rc < 0) {
1960                         ll_put_grouplock(llss->inode1, file1, gid);
1961                         GOTO(free, rc);
1962                 }
1963         }
1964
1965         /* to be able to restore mtime and atime after swap
1966          * we need to first save them */
1967         if (lsl->sl_flags &
1968             (SWAP_LAYOUTS_KEEP_MTIME | SWAP_LAYOUTS_KEEP_ATIME)) {
1969                 llss->ia1.ia_mtime = llss->inode1->i_mtime;
1970                 llss->ia1.ia_atime = llss->inode1->i_atime;
1971                 llss->ia1.ia_valid = ATTR_MTIME | ATTR_ATIME;
1972                 llss->ia2.ia_mtime = llss->inode2->i_mtime;
1973                 llss->ia2.ia_atime = llss->inode2->i_atime;
1974                 llss->ia2.ia_valid = ATTR_MTIME | ATTR_ATIME;
1975         }
1976
1977         /* ultimate check, before swaping the layouts we check if
1978          * dataversion has changed (if requested) */
1979         if (llss->check_dv1) {
1980                 rc = ll_data_version(llss->inode1, &dv, 0);
1981                 if (rc)
1982                         GOTO(putgl, rc);
1983                 if (dv != llss->dv1)
1984                         GOTO(putgl, rc = -EAGAIN);
1985         }
1986
1987         if (llss->check_dv2) {
1988                 rc = ll_data_version(llss->inode2, &dv, 0);
1989                 if (rc)
1990                         GOTO(putgl, rc);
1991                 if (dv != llss->dv2)
1992                         GOTO(putgl, rc = -EAGAIN);
1993         }
1994
1995         /* struct md_op_data is used to send the swap args to the mdt
1996          * only flags is missing, so we use struct mdc_swap_layouts
1997          * through the md_op_data->op_data */
1998         /* flags from user space have to be converted before they are send to
1999          * server, no flag is sent today, they are only used on the client */
2000         msl.msl_flags = 0;
2001         rc = -ENOMEM;
2002         op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2003                                      0, LUSTRE_OPC_ANY, &msl);
2004         if (IS_ERR(op_data))
2005                 GOTO(free, rc = PTR_ERR(op_data));
2006
2007         rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2008                            sizeof(*op_data), op_data, NULL);
2009         ll_finish_md_op_data(op_data);
2010
2011 putgl:
2012         if (gid != 0) {
2013                 ll_put_grouplock(llss->inode2, file2, gid);
2014                 ll_put_grouplock(llss->inode1, file1, gid);
2015         }
2016
2017         /* rc can be set from obd_iocontrol() or from a GOTO(putgl, ...) */
2018         if (rc != 0)
2019                 GOTO(free, rc);
2020
2021         /* clear useless flags */
2022         if (!(lsl->sl_flags & SWAP_LAYOUTS_KEEP_MTIME)) {
2023                 llss->ia1.ia_valid &= ~ATTR_MTIME;
2024                 llss->ia2.ia_valid &= ~ATTR_MTIME;
2025         }
2026
2027         if (!(lsl->sl_flags & SWAP_LAYOUTS_KEEP_ATIME)) {
2028                 llss->ia1.ia_valid &= ~ATTR_ATIME;
2029                 llss->ia2.ia_valid &= ~ATTR_ATIME;
2030         }
2031
2032         /* update time if requested */
2033         rc = 0;
2034         if (llss->ia2.ia_valid != 0) {
2035                 mutex_lock(&llss->inode1->i_mutex);
2036                 rc = ll_setattr(file1->f_dentry, &llss->ia2);
2037                 mutex_unlock(&llss->inode1->i_mutex);
2038         }
2039
2040         if (llss->ia1.ia_valid != 0) {
2041                 int rc1;
2042
2043                 mutex_lock(&llss->inode2->i_mutex);
2044                 rc1 = ll_setattr(file2->f_dentry, &llss->ia1);
2045                 mutex_unlock(&llss->inode2->i_mutex);
2046                 if (rc == 0)
2047                         rc = rc1;
2048         }
2049
2050 free:
2051         if (llss != NULL)
2052                 OBD_FREE_PTR(llss);
2053
2054         return rc;
2055 }
2056
2057 long ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2058 {
2059         struct inode            *inode = file->f_dentry->d_inode;
2060         struct ll_file_data     *fd = LUSTRE_FPRIVATE(file);
2061         int                      flags, rc;
2062
2063         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
2064                inode->i_generation, inode, cmd);
2065         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
2066
2067         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
2068         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
2069                 return -ENOTTY;
2070
2071         switch(cmd) {
2072         case LL_IOC_GETFLAGS:
2073                 /* Get the current value of the file flags */
2074                 return put_user(fd->fd_flags, (int *)arg);
2075         case LL_IOC_SETFLAGS:
2076         case LL_IOC_CLRFLAGS:
2077                 /* Set or clear specific file flags */
2078                 /* XXX This probably needs checks to ensure the flags are
2079                  *     not abused, and to handle any flag side effects.
2080                  */
2081                 if (get_user(flags, (int *) arg))
2082                         return -EFAULT;
2083
2084                 if (cmd == LL_IOC_SETFLAGS) {
2085                         if ((flags & LL_FILE_IGNORE_LOCK) &&
2086                             !(file->f_flags & O_DIRECT)) {
2087                                 CERROR("%s: unable to disable locking on "
2088                                        "non-O_DIRECT file\n", current->comm);
2089                                 return -EINVAL;
2090                         }
2091
2092                         fd->fd_flags |= flags;
2093                 } else {
2094                         fd->fd_flags &= ~flags;
2095                 }
2096                 return 0;
2097         case LL_IOC_LOV_SETSTRIPE:
2098                 return ll_lov_setstripe(inode, file, arg);
2099         case LL_IOC_LOV_SETEA:
2100                 return ll_lov_setea(inode, file, arg);
2101         case LL_IOC_LOV_SWAP_LAYOUTS: {
2102                 struct file *file2;
2103                 struct lustre_swap_layouts lsl;
2104
2105                 if (copy_from_user(&lsl, (char *)arg,
2106                                        sizeof(struct lustre_swap_layouts)))
2107                         return -EFAULT;
2108
2109                 if ((file->f_flags & O_ACCMODE) == 0) /* O_RDONLY */
2110                         return -EPERM;
2111
2112                 file2 = fget(lsl.sl_fd);
2113                 if (file2 == NULL)
2114                         return -EBADF;
2115
2116                 rc = -EPERM;
2117                 if ((file2->f_flags & O_ACCMODE) != 0) /* O_WRONLY or O_RDWR */
2118                         rc = ll_swap_layouts(file, file2, &lsl);
2119                 fput(file2);
2120                 return rc;
2121         }
2122         case LL_IOC_LOV_GETSTRIPE:
2123                 return ll_lov_getstripe(inode, arg);
2124         case LL_IOC_RECREATE_OBJ:
2125                 return ll_lov_recreate_obj(inode, arg);
2126         case LL_IOC_RECREATE_FID:
2127                 return ll_lov_recreate_fid(inode, arg);
2128         case FSFILT_IOC_FIEMAP:
2129                 return ll_ioctl_fiemap(inode, arg);
2130         case FSFILT_IOC_GETFLAGS:
2131         case FSFILT_IOC_SETFLAGS:
2132                 return ll_iocontrol(inode, file, cmd, arg);
2133         case FSFILT_IOC_GETVERSION_OLD:
2134         case FSFILT_IOC_GETVERSION:
2135                 return put_user(inode->i_generation, (int *)arg);
2136         case LL_IOC_GROUP_LOCK:
2137                 return ll_get_grouplock(inode, file, arg);
2138         case LL_IOC_GROUP_UNLOCK:
2139                 return ll_put_grouplock(inode, file, arg);
2140         case IOC_OBD_STATFS:
2141                 return ll_obd_statfs(inode, (void *)arg);
2142
2143         /* We need to special case any other ioctls we want to handle,
2144          * to send them to the MDS/OST as appropriate and to properly
2145          * network encode the arg field.
2146         case FSFILT_IOC_SETVERSION_OLD:
2147         case FSFILT_IOC_SETVERSION:
2148         */
2149         case LL_IOC_FLUSHCTX:
2150                 return ll_flush_ctx(inode);
2151         case LL_IOC_PATH2FID: {
2152                 if (copy_to_user((void *)arg, ll_inode2fid(inode),
2153                                  sizeof(struct lu_fid)))
2154                         return -EFAULT;
2155
2156                 return 0;
2157         }
2158         case OBD_IOC_FID2PATH:
2159                 return ll_fid2path(inode, (void *)arg);
2160         case LL_IOC_DATA_VERSION: {
2161                 struct ioc_data_version idv;
2162                 int                     rc;
2163
2164                 if (copy_from_user(&idv, (char *)arg, sizeof(idv)))
2165                         return -EFAULT;
2166
2167                 rc = ll_data_version(inode, &idv.idv_version,
2168                                 !(idv.idv_flags & LL_DV_NOFLUSH));
2169
2170                 if (rc == 0 && copy_to_user((char *) arg, &idv, sizeof(idv)))
2171                         return -EFAULT;
2172
2173                 return rc;
2174         }
2175
2176         case LL_IOC_GET_MDTIDX: {
2177                 int mdtidx;
2178
2179                 mdtidx = ll_get_mdt_idx(inode);
2180                 if (mdtidx < 0)
2181                         return mdtidx;
2182
2183                 if (put_user((int)mdtidx, (int*)arg))
2184                         return -EFAULT;
2185
2186                 return 0;
2187         }
2188         case OBD_IOC_GETDTNAME:
2189         case OBD_IOC_GETMDNAME:
2190                 return ll_get_obd_name(inode, cmd, arg);
2191         case LL_IOC_HSM_STATE_GET: {
2192                 struct md_op_data       *op_data;
2193                 struct hsm_user_state   *hus;
2194                 int                      rc;
2195
2196                 OBD_ALLOC_PTR(hus);
2197                 if (hus == NULL)
2198                         return -ENOMEM;
2199
2200                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2201                                              LUSTRE_OPC_ANY, hus);
2202                 if (IS_ERR(op_data)) {
2203                         OBD_FREE_PTR(hus);
2204                         return PTR_ERR(op_data);
2205                 }
2206
2207                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
2208                                    op_data, NULL);
2209
2210                 if (copy_to_user((void *)arg, hus, sizeof(*hus)))
2211                         rc = -EFAULT;
2212
2213                 ll_finish_md_op_data(op_data);
2214                 OBD_FREE_PTR(hus);
2215                 return rc;
2216         }
2217         case LL_IOC_HSM_STATE_SET: {
2218                 struct md_op_data       *op_data;
2219                 struct hsm_state_set    *hss;
2220                 int                      rc;
2221
2222                 OBD_ALLOC_PTR(hss);
2223                 if (hss == NULL)
2224                         return -ENOMEM;
2225                 if (copy_from_user(hss, (char *)arg, sizeof(*hss))) {
2226                         OBD_FREE_PTR(hss);
2227                         return -EFAULT;
2228                 }
2229
2230                 /* Non-root users are forbidden to set or clear flags which are
2231                  * NOT defined in HSM_USER_MASK. */
2232                 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK)
2233                     && !cfs_capable(CFS_CAP_SYS_ADMIN)) {
2234                         OBD_FREE_PTR(hss);
2235                         return -EPERM;
2236                 }
2237
2238                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2239                                              LUSTRE_OPC_ANY, hss);
2240                 if (IS_ERR(op_data)) {
2241                         OBD_FREE_PTR(hss);
2242                         return PTR_ERR(op_data);
2243                 }
2244
2245                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
2246                                    op_data, NULL);
2247
2248                 ll_finish_md_op_data(op_data);
2249
2250                 OBD_FREE_PTR(hss);
2251                 return rc;
2252         }
2253         case LL_IOC_HSM_ACTION: {
2254                 struct md_op_data               *op_data;
2255                 struct hsm_current_action       *hca;
2256                 int                              rc;
2257
2258                 OBD_ALLOC_PTR(hca);
2259                 if (hca == NULL)
2260                         return -ENOMEM;
2261
2262                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2263                                              LUSTRE_OPC_ANY, hca);
2264                 if (IS_ERR(op_data)) {
2265                         OBD_FREE_PTR(hca);
2266                         return PTR_ERR(op_data);
2267                 }
2268
2269                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
2270                                    op_data, NULL);
2271
2272                 if (copy_to_user((char *)arg, hca, sizeof(*hca)))
2273                         rc = -EFAULT;
2274
2275                 ll_finish_md_op_data(op_data);
2276                 OBD_FREE_PTR(hca);
2277                 return rc;
2278         }
2279         case LL_IOC_SET_LEASE: {
2280                 struct ll_inode_info *lli = ll_i2info(inode);
2281                 struct obd_client_handle *och = NULL;
2282                 bool lease_broken;
2283                 fmode_t mode = 0;
2284
2285                 switch (arg) {
2286                 case F_WRLCK:
2287                         if (!(file->f_mode & FMODE_WRITE))
2288                                 return -EPERM;
2289                         mode = FMODE_WRITE;
2290                         break;
2291                 case F_RDLCK:
2292                         if (!(file->f_mode & FMODE_READ))
2293                                 return -EPERM;
2294                         mode = FMODE_READ;
2295                         break;
2296                 case F_UNLCK:
2297                         mutex_lock(&lli->lli_och_mutex);
2298                         if (fd->fd_lease_och != NULL) {
2299                                 och = fd->fd_lease_och;
2300                                 fd->fd_lease_och = NULL;
2301                         }
2302                         mutex_unlock(&lli->lli_och_mutex);
2303
2304                         if (och != NULL) {
2305                                 mode = och->och_flags &
2306                                        (FMODE_READ|FMODE_WRITE);
2307                                 rc = ll_lease_close(och, inode, &lease_broken);
2308                                 if (rc == 0 && lease_broken)
2309                                         mode = 0;
2310                         } else {
2311                                 rc = -ENOLCK;
2312                         }
2313
2314                         /* return the type of lease or error */
2315                         return rc < 0 ? rc : (int)mode;
2316                 default:
2317                         return -EINVAL;
2318                 }
2319
2320                 CDEBUG(D_INODE, "Set lease with mode %d\n", mode);
2321
2322                 /* apply for lease */
2323                 och = ll_lease_open(inode, file, mode);
2324                 if (IS_ERR(och))
2325                         return PTR_ERR(och);
2326
2327                 rc = 0;
2328                 mutex_lock(&lli->lli_och_mutex);
2329                 if (fd->fd_lease_och == NULL) {
2330                         fd->fd_lease_och = och;
2331                         och = NULL;
2332                 }
2333                 mutex_unlock(&lli->lli_och_mutex);
2334                 if (och != NULL) {
2335                         /* impossible now that only excl is supported for now */
2336                         ll_lease_close(och, inode, &lease_broken);
2337                         rc = -EBUSY;
2338                 }
2339                 return rc;
2340         }
2341         case LL_IOC_GET_LEASE: {
2342                 struct ll_inode_info *lli = ll_i2info(inode);
2343                 struct ldlm_lock *lock = NULL;
2344
2345                 rc = 0;
2346                 mutex_lock(&lli->lli_och_mutex);
2347                 if (fd->fd_lease_och != NULL) {
2348                         struct obd_client_handle *och = fd->fd_lease_och;
2349
2350                         lock = ldlm_handle2lock(&och->och_lease_handle);
2351                         if (lock != NULL) {
2352                                 lock_res_and_lock(lock);
2353                                 if (!ldlm_is_cancel(lock))
2354                                         rc = och->och_flags &
2355                                                 (FMODE_READ | FMODE_WRITE);
2356                                 unlock_res_and_lock(lock);
2357                                 ldlm_lock_put(lock);
2358                         }
2359                 }
2360                 mutex_unlock(&lli->lli_och_mutex);
2361
2362                 return rc;
2363         }
2364         default: {
2365                 int err;
2366
2367                 if (LLIOC_STOP ==
2368                      ll_iocontrol_call(inode, file, cmd, arg, &err))
2369                         return err;
2370
2371                 return obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
2372                                      (void *)arg);
2373         }
2374         }
2375 }
2376
2377
2378 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
2379 {
2380         struct inode *inode = file->f_dentry->d_inode;
2381         loff_t retval, eof = 0;
2382
2383         retval = offset + ((origin == SEEK_END) ? i_size_read(inode) :
2384                            (origin == SEEK_CUR) ? file->f_pos : 0);
2385         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), to=%llu=%#llx(%d)\n",
2386                inode->i_ino, inode->i_generation, inode, retval, retval,
2387                origin);
2388         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK, 1);
2389
2390         if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) {
2391                 retval = ll_glimpse_size(inode);
2392                 if (retval != 0)
2393                         return retval;
2394                 eof = i_size_read(inode);
2395         }
2396
2397         retval = generic_file_llseek_size(file, offset, origin,
2398                                           ll_file_maxbytes(inode), eof);
2399         return retval;
2400 }
2401
2402 int ll_flush(struct file *file, fl_owner_t id)
2403 {
2404         struct inode *inode = file->f_dentry->d_inode;
2405         struct ll_inode_info *lli = ll_i2info(inode);
2406         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2407         int rc, err;
2408
2409         LASSERT(!S_ISDIR(inode->i_mode));
2410
2411         /* catch async errors that were recorded back when async writeback
2412          * failed for pages in this mapping. */
2413         rc = lli->lli_async_rc;
2414         lli->lli_async_rc = 0;
2415         err = lov_read_and_clear_async_rc(lli->lli_clob);
2416         if (rc == 0)
2417                 rc = err;
2418
2419         /* The application has been told write failure already.
2420          * Do not report failure again. */
2421         if (fd->fd_write_failed)
2422                 return 0;
2423         return rc ? -EIO : 0;
2424 }
2425
2426 /**
2427  * Called to make sure a portion of file has been written out.
2428  * if @local_only is not true, it will send OST_SYNC RPCs to ost.
2429  *
2430  * Return how many pages have been written.
2431  */
2432 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
2433                        enum cl_fsync_mode mode, int ignore_layout)
2434 {
2435         struct cl_env_nest nest;
2436         struct lu_env *env;
2437         struct cl_io *io;
2438         struct obd_capa *capa = NULL;
2439         struct cl_fsync_io *fio;
2440         int result;
2441
2442         if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
2443             mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
2444                 return -EINVAL;
2445
2446         env = cl_env_nested_get(&nest);
2447         if (IS_ERR(env))
2448                 return PTR_ERR(env);
2449
2450         capa = ll_osscapa_get(inode, CAPA_OPC_OSS_WRITE);
2451
2452         io = ccc_env_thread_io(env);
2453         io->ci_obj = cl_i2info(inode)->lli_clob;
2454         io->ci_ignore_layout = ignore_layout;
2455
2456         /* initialize parameters for sync */
2457         fio = &io->u.ci_fsync;
2458         fio->fi_capa = capa;
2459         fio->fi_start = start;
2460         fio->fi_end = end;
2461         fio->fi_fid = ll_inode2fid(inode);
2462         fio->fi_mode = mode;
2463         fio->fi_nr_written = 0;
2464
2465         if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
2466                 result = cl_io_loop(env, io);
2467         else
2468                 result = io->ci_result;
2469         if (result == 0)
2470                 result = fio->fi_nr_written;
2471         cl_io_fini(env, io);
2472         cl_env_nested_put(&nest, env);
2473
2474         capa_put(capa);
2475
2476         return result;
2477 }
2478
2479 /*
2480  * When dentry is provided (the 'else' case), *file->f_dentry may be
2481  * null and dentry must be used directly rather than pulled from
2482  * *file->f_dentry as is done otherwise.
2483  */
2484
2485 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2486 {
2487         struct dentry *dentry = file->f_dentry;
2488         struct inode *inode = dentry->d_inode;
2489         struct ll_inode_info *lli = ll_i2info(inode);
2490         struct ptlrpc_request *req;
2491         struct obd_capa *oc;
2492         int rc, err;
2493
2494         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
2495                inode->i_generation, inode);
2496         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC, 1);
2497
2498         rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
2499         mutex_lock(&inode->i_mutex);
2500
2501         /* catch async errors that were recorded back when async writeback
2502          * failed for pages in this mapping. */
2503         if (!S_ISDIR(inode->i_mode)) {
2504                 err = lli->lli_async_rc;
2505                 lli->lli_async_rc = 0;
2506                 if (rc == 0)
2507                         rc = err;
2508                 err = lov_read_and_clear_async_rc(lli->lli_clob);
2509                 if (rc == 0)
2510                         rc = err;
2511         }
2512
2513         oc = ll_mdscapa_get(inode);
2514         err = md_sync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), oc,
2515                       &req);
2516         capa_put(oc);
2517         if (!rc)
2518                 rc = err;
2519         if (!err)
2520                 ptlrpc_req_finished(req);
2521
2522         if (datasync && S_ISREG(inode->i_mode)) {
2523                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2524
2525                 err = cl_sync_file_range(inode, 0, OBD_OBJECT_EOF,
2526                                 CL_FSYNC_ALL, 0);
2527                 if (rc == 0 && err < 0)
2528                         rc = err;
2529                 if (rc < 0)
2530                         fd->fd_write_failed = true;
2531                 else
2532                         fd->fd_write_failed = false;
2533         }
2534
2535         mutex_unlock(&inode->i_mutex);
2536         return rc;
2537 }
2538
2539 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
2540 {
2541         struct inode *inode = file->f_dentry->d_inode;
2542         struct ll_sb_info *sbi = ll_i2sbi(inode);
2543         struct ldlm_enqueue_info einfo = {
2544                 .ei_type        = LDLM_FLOCK,
2545                 .ei_cb_cp       = ldlm_flock_completion_ast,
2546                 .ei_cbdata      = file_lock,
2547         };
2548         struct md_op_data *op_data;
2549         struct lustre_handle lockh = {0};
2550         ldlm_policy_data_t flock = {{0}};
2551         int flags = 0;
2552         int rc;
2553         int rc2 = 0;
2554
2555         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
2556                inode->i_ino, file_lock);
2557
2558         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK, 1);
2559
2560         if (file_lock->fl_flags & FL_FLOCK) {
2561                 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
2562                 /* flocks are whole-file locks */
2563                 flock.l_flock.end = OFFSET_MAX;
2564                 /* For flocks owner is determined by the local file desctiptor*/
2565                 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
2566         } else if (file_lock->fl_flags & FL_POSIX) {
2567                 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
2568                 flock.l_flock.start = file_lock->fl_start;
2569                 flock.l_flock.end = file_lock->fl_end;
2570         } else {
2571                 return -EINVAL;
2572         }
2573         flock.l_flock.pid = file_lock->fl_pid;
2574
2575         /* Somewhat ugly workaround for svc lockd.
2576          * lockd installs custom fl_lmops->lm_compare_owner that checks
2577          * for the fl_owner to be the same (which it always is on local node
2578          * I guess between lockd processes) and then compares pid.
2579          * As such we assign pid to the owner field to make it all work,
2580          * conflict with normal locks is unlikely since pid space and
2581          * pointer space for current->files are not intersecting */
2582         if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
2583                 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
2584
2585         switch (file_lock->fl_type) {
2586         case F_RDLCK:
2587                 einfo.ei_mode = LCK_PR;
2588                 break;
2589         case F_UNLCK:
2590                 /* An unlock request may or may not have any relation to
2591                  * existing locks so we may not be able to pass a lock handle
2592                  * via a normal ldlm_lock_cancel() request. The request may even
2593                  * unlock a byte range in the middle of an existing lock. In
2594                  * order to process an unlock request we need all of the same
2595                  * information that is given with a normal read or write record
2596                  * lock request. To avoid creating another ldlm unlock (cancel)
2597                  * message we'll treat a LCK_NL flock request as an unlock. */
2598                 einfo.ei_mode = LCK_NL;
2599                 break;
2600         case F_WRLCK:
2601                 einfo.ei_mode = LCK_PW;
2602                 break;
2603         default:
2604                 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n",
2605                         file_lock->fl_type);
2606                 return -ENOTSUPP;
2607         }
2608
2609         switch (cmd) {
2610         case F_SETLKW:
2611 #ifdef F_SETLKW64
2612         case F_SETLKW64:
2613 #endif
2614                 flags = 0;
2615                 break;
2616         case F_SETLK:
2617 #ifdef F_SETLK64
2618         case F_SETLK64:
2619 #endif
2620                 flags = LDLM_FL_BLOCK_NOWAIT;
2621                 break;
2622         case F_GETLK:
2623 #ifdef F_GETLK64
2624         case F_GETLK64:
2625 #endif
2626                 flags = LDLM_FL_TEST_LOCK;
2627                 /* Save the old mode so that if the mode in the lock changes we
2628                  * can decrement the appropriate reader or writer refcount. */
2629                 file_lock->fl_type = einfo.ei_mode;
2630                 break;
2631         default:
2632                 CERROR("unknown fcntl lock command: %d\n", cmd);
2633                 return -EINVAL;
2634         }
2635
2636         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2637                                      LUSTRE_OPC_ANY, NULL);
2638         if (IS_ERR(op_data))
2639                 return PTR_ERR(op_data);
2640
2641         CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
2642                "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
2643                flags, einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
2644
2645         rc = md_enqueue(sbi->ll_md_exp, &einfo, NULL,
2646                         op_data, &lockh, &flock, 0, NULL /* req */, flags);
2647
2648         if ((file_lock->fl_flags & FL_FLOCK) &&
2649             (rc == 0 || file_lock->fl_type == F_UNLCK))
2650                 rc2  = flock_lock_file_wait(file, file_lock);
2651         if ((file_lock->fl_flags & FL_POSIX) &&
2652             (rc == 0 || file_lock->fl_type == F_UNLCK) &&
2653             !(flags & LDLM_FL_TEST_LOCK))
2654                 rc2  = posix_lock_file_wait(file, file_lock);
2655
2656         if (rc2 && file_lock->fl_type != F_UNLCK) {
2657                 einfo.ei_mode = LCK_NL;
2658                 md_enqueue(sbi->ll_md_exp, &einfo, NULL,
2659                         op_data, &lockh, &flock, 0, NULL /* req */, flags);
2660                 rc = rc2;
2661         }
2662
2663         ll_finish_md_op_data(op_data);
2664
2665         return rc;
2666 }
2667
2668 int ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
2669 {
2670         return -ENOSYS;
2671 }
2672
2673 /**
2674  * test if some locks matching bits and l_req_mode are acquired
2675  * - bits can be in different locks
2676  * - if found clear the common lock bits in *bits
2677  * - the bits not found, are kept in *bits
2678  * \param inode [IN]
2679  * \param bits [IN] searched lock bits [IN]
2680  * \param l_req_mode [IN] searched lock mode
2681  * \retval boolean, true iff all bits are found
2682  */
2683 int ll_have_md_lock(struct inode *inode, __u64 *bits,  ldlm_mode_t l_req_mode)
2684 {
2685         struct lustre_handle lockh;
2686         ldlm_policy_data_t policy;
2687         ldlm_mode_t mode = (l_req_mode == LCK_MINMODE) ?
2688                                 (LCK_CR|LCK_CW|LCK_PR|LCK_PW) : l_req_mode;
2689         struct lu_fid *fid;
2690         __u64 flags;
2691         int i;
2692
2693         if (!inode)
2694                return 0;
2695
2696         fid = &ll_i2info(inode)->lli_fid;
2697         CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
2698                ldlm_lockname[mode]);
2699
2700         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
2701         for (i = 0; i <= MDS_INODELOCK_MAXSHIFT && *bits != 0; i++) {
2702                 policy.l_inodebits.bits = *bits & (1 << i);
2703                 if (policy.l_inodebits.bits == 0)
2704                         continue;
2705
2706                 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
2707                                   &policy, mode, &lockh)) {
2708                         struct ldlm_lock *lock;
2709
2710                         lock = ldlm_handle2lock(&lockh);
2711                         if (lock) {
2712                                 *bits &=
2713                                       ~(lock->l_policy_data.l_inodebits.bits);
2714                                 LDLM_LOCK_PUT(lock);
2715                         } else {
2716                                 *bits &= ~policy.l_inodebits.bits;
2717                         }
2718                 }
2719         }
2720         return *bits == 0;
2721 }
2722
2723 ldlm_mode_t ll_take_md_lock(struct inode *inode, __u64 bits,
2724                             struct lustre_handle *lockh, __u64 flags)
2725 {
2726         ldlm_policy_data_t policy = { .l_inodebits = {bits}};
2727         struct lu_fid *fid;
2728         ldlm_mode_t rc;
2729
2730         fid = &ll_i2info(inode)->lli_fid;
2731         CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
2732
2733         rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
2734                            fid, LDLM_IBITS, &policy,
2735                            LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
2736         return rc;
2737 }
2738
2739 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
2740 {
2741         /* Already unlinked. Just update nlink and return success */
2742         if (rc == -ENOENT) {
2743                 clear_nlink(inode);
2744                 /* This path cannot be hit for regular files unless in
2745                  * case of obscure races, so no need to validate size.
2746                  */
2747                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
2748                         return 0;
2749         } else if (rc != 0) {
2750                 CERROR("%s: revalidate FID "DFID" error: rc = %d\n",
2751                        ll_get_fsname(inode->i_sb, NULL, 0),
2752                        PFID(ll_inode2fid(inode)), rc);
2753         }
2754
2755         return rc;
2756 }
2757
2758 int __ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2759                              __u64 ibits)
2760 {
2761         struct inode *inode = dentry->d_inode;
2762         struct ptlrpc_request *req = NULL;
2763         struct obd_export *exp;
2764         int rc = 0;
2765
2766         LASSERT(inode != NULL);
2767
2768         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
2769                inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
2770
2771         exp = ll_i2mdexp(inode);
2772
2773         /* XXX: Enable OBD_CONNECT_ATTRFID to reduce unnecessary getattr RPC.
2774          *      But under CMD case, it caused some lock issues, should be fixed
2775          *      with new CMD ibits lock. See bug 12718 */
2776         if (exp_connect_flags(exp) & OBD_CONNECT_ATTRFID) {
2777                 struct lookup_intent oit = { .it_op = IT_GETATTR };
2778                 struct md_op_data *op_data;
2779
2780                 if (ibits == MDS_INODELOCK_LOOKUP)
2781                         oit.it_op = IT_LOOKUP;
2782
2783                 /* Call getattr by fid, so do not provide name at all. */
2784                 op_data = ll_prep_md_op_data(NULL, dentry->d_parent->d_inode,
2785                                              dentry->d_inode, NULL, 0, 0,
2786                                              LUSTRE_OPC_ANY, NULL);
2787                 if (IS_ERR(op_data))
2788                         return PTR_ERR(op_data);
2789
2790                 oit.it_create_mode |= M_CHECK_STALE;
2791                 rc = md_intent_lock(exp, op_data, NULL, 0,
2792                                     /* we are not interested in name
2793                                        based lookup */
2794                                     &oit, 0, &req,
2795                                     ll_md_blocking_ast, 0);
2796                 ll_finish_md_op_data(op_data);
2797                 oit.it_create_mode &= ~M_CHECK_STALE;
2798                 if (rc < 0) {
2799                         rc = ll_inode_revalidate_fini(inode, rc);
2800                         GOTO (out, rc);
2801                 }
2802
2803                 rc = ll_revalidate_it_finish(req, &oit, dentry);
2804                 if (rc != 0) {
2805                         ll_intent_release(&oit);
2806                         GOTO(out, rc);
2807                 }
2808
2809                 /* Unlinked? Unhash dentry, so it is not picked up later by
2810                    do_lookup() -> ll_revalidate_it(). We cannot use d_drop
2811                    here to preserve get_cwd functionality on 2.6.
2812                    Bug 10503 */
2813                 if (!dentry->d_inode->i_nlink)
2814                         d_lustre_invalidate(dentry, 0);
2815
2816                 ll_lookup_finish_locks(&oit, dentry);
2817         } else if (!ll_have_md_lock(dentry->d_inode, &ibits, LCK_MINMODE)) {
2818                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
2819                 obd_valid valid = OBD_MD_FLGETATTR;
2820                 struct md_op_data *op_data;
2821                 int ealen = 0;
2822
2823                 if (S_ISREG(inode->i_mode)) {
2824                         rc = ll_get_max_mdsize(sbi, &ealen);
2825                         if (rc)
2826                                 return rc;
2827                         valid |= OBD_MD_FLEASIZE | OBD_MD_FLMODEASIZE;
2828                 }
2829
2830                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2831                                              0, ealen, LUSTRE_OPC_ANY,
2832                                              NULL);
2833                 if (IS_ERR(op_data))
2834                         return PTR_ERR(op_data);
2835
2836                 op_data->op_valid = valid;
2837                 /* Once OBD_CONNECT_ATTRFID is not supported, we can't find one
2838                  * capa for this inode. Because we only keep capas of dirs
2839                  * fresh. */
2840                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2841                 ll_finish_md_op_data(op_data);
2842                 if (rc) {
2843                         rc = ll_inode_revalidate_fini(inode, rc);
2844                         return rc;
2845                 }
2846
2847                 rc = ll_prep_inode(&inode, req, NULL, NULL);
2848         }
2849 out:
2850         ptlrpc_req_finished(req);
2851         return rc;
2852 }
2853
2854 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it,
2855                            __u64 ibits)
2856 {
2857         struct inode *inode = dentry->d_inode;
2858         int rc;
2859
2860         rc = __ll_inode_revalidate_it(dentry, it, ibits);
2861         if (rc != 0)
2862                 return rc;
2863
2864         /* if object isn't regular file, don't validate size */
2865         if (!S_ISREG(inode->i_mode)) {
2866                 LTIME_S(inode->i_atime) = ll_i2info(inode)->lli_lvb.lvb_atime;
2867                 LTIME_S(inode->i_mtime) = ll_i2info(inode)->lli_lvb.lvb_mtime;
2868                 LTIME_S(inode->i_ctime) = ll_i2info(inode)->lli_lvb.lvb_ctime;
2869         } else {
2870                 /* In case of restore, the MDT has the right size and has
2871                  * already send it back without granting the layout lock,
2872                  * inode is up-to-date so glimpse is useless.
2873                  * Also to glimpse we need the layout, in case of a running
2874                  * restore the MDT holds the layout lock so the glimpse will
2875                  * block up to the end of restore (getattr will block)
2876                  */
2877                 if (!(ll_i2info(inode)->lli_flags & LLIF_FILE_RESTORING))
2878                         rc = ll_glimpse_size(inode);
2879         }
2880         return rc;
2881 }
2882
2883 int ll_getattr_it(struct vfsmount *mnt, struct dentry *de,
2884                   struct lookup_intent *it, struct kstat *stat)
2885 {
2886         struct inode *inode = de->d_inode;
2887         struct ll_sb_info *sbi = ll_i2sbi(inode);
2888         struct ll_inode_info *lli = ll_i2info(inode);
2889         int res = 0;
2890
2891         res = ll_inode_revalidate_it(de, it, MDS_INODELOCK_UPDATE |
2892                                              MDS_INODELOCK_LOOKUP);
2893         ll_stats_ops_tally(sbi, LPROC_LL_GETATTR, 1);
2894
2895         if (res)
2896                 return res;
2897
2898         stat->dev = inode->i_sb->s_dev;
2899         if (ll_need_32bit_api(sbi))
2900                 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
2901         else
2902                 stat->ino = inode->i_ino;
2903         stat->mode = inode->i_mode;
2904         stat->nlink = inode->i_nlink;
2905         stat->uid = inode->i_uid;
2906         stat->gid = inode->i_gid;
2907         stat->rdev = inode->i_rdev;
2908         stat->atime = inode->i_atime;
2909         stat->mtime = inode->i_mtime;
2910         stat->ctime = inode->i_ctime;
2911         stat->blksize = 1 << inode->i_blkbits;
2912
2913         stat->size = i_size_read(inode);
2914         stat->blocks = inode->i_blocks;
2915
2916         return 0;
2917 }
2918 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
2919 {
2920         struct lookup_intent it = { .it_op = IT_GETATTR };
2921
2922         return ll_getattr_it(mnt, de, &it, stat);
2923 }
2924
2925 int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2926                 __u64 start, __u64 len)
2927 {
2928         int rc;
2929         size_t num_bytes;
2930         struct ll_user_fiemap *fiemap;
2931         unsigned int extent_count = fieinfo->fi_extents_max;
2932
2933         num_bytes = sizeof(*fiemap) + (extent_count *
2934                                        sizeof(struct ll_fiemap_extent));
2935         OBD_ALLOC_LARGE(fiemap, num_bytes);
2936
2937         if (fiemap == NULL)
2938                 return -ENOMEM;
2939
2940         fiemap->fm_flags = fieinfo->fi_flags;
2941         fiemap->fm_extent_count = fieinfo->fi_extents_max;
2942         fiemap->fm_start = start;
2943         fiemap->fm_length = len;
2944         memcpy(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
2945                sizeof(struct ll_fiemap_extent));
2946
2947         rc = ll_do_fiemap(inode, fiemap, num_bytes);
2948
2949         fieinfo->fi_flags = fiemap->fm_flags;
2950         fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
2951         memcpy(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
2952                fiemap->fm_mapped_extents * sizeof(struct ll_fiemap_extent));
2953
2954         OBD_FREE_LARGE(fiemap, num_bytes);
2955         return rc;
2956 }
2957
2958 struct posix_acl * ll_get_acl(struct inode *inode, int type)
2959 {
2960         struct ll_inode_info *lli = ll_i2info(inode);
2961         struct posix_acl *acl = NULL;
2962
2963         spin_lock(&lli->lli_lock);
2964         /* VFS' acl_permission_check->check_acl will release the refcount */
2965         acl = posix_acl_dup(lli->lli_posix_acl);
2966         spin_unlock(&lli->lli_lock);
2967
2968         return acl;
2969 }
2970
2971
2972 int ll_inode_permission(struct inode *inode, int mask)
2973 {
2974         int rc = 0;
2975
2976 #ifdef MAY_NOT_BLOCK
2977         if (mask & MAY_NOT_BLOCK)
2978                 return -ECHILD;
2979 #endif
2980
2981        /* as root inode are NOT getting validated in lookup operation,
2982         * need to do it before permission check. */
2983
2984         if (inode == inode->i_sb->s_root->d_inode) {
2985                 struct lookup_intent it = { .it_op = IT_LOOKUP };
2986
2987                 rc = __ll_inode_revalidate_it(inode->i_sb->s_root, &it,
2988                                               MDS_INODELOCK_LOOKUP);
2989                 if (rc)
2990                         return rc;
2991         }
2992
2993         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), inode mode %x mask %o\n",
2994                inode->i_ino, inode->i_generation, inode, inode->i_mode, mask);
2995
2996         if (ll_i2sbi(inode)->ll_flags & LL_SBI_RMT_CLIENT)
2997                 return lustre_check_remote_perm(inode, mask);
2998
2999         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_PERM, 1);
3000         rc = generic_permission(inode, mask);
3001
3002         return rc;
3003 }
3004
3005 /* -o localflock - only provides locally consistent flock locks */
3006 struct file_operations ll_file_operations = {
3007         .read      = ll_file_read,
3008         .aio_read = ll_file_aio_read,
3009         .write    = ll_file_write,
3010         .aio_write = ll_file_aio_write,
3011         .unlocked_ioctl = ll_file_ioctl,
3012         .open      = ll_file_open,
3013         .release        = ll_file_release,
3014         .mmap      = ll_file_mmap,
3015         .llseek  = ll_file_seek,
3016         .splice_read    = ll_file_splice_read,
3017         .fsync    = ll_fsync,
3018         .flush    = ll_flush
3019 };
3020
3021 struct file_operations ll_file_operations_flock = {
3022         .read      = ll_file_read,
3023         .aio_read    = ll_file_aio_read,
3024         .write    = ll_file_write,
3025         .aio_write   = ll_file_aio_write,
3026         .unlocked_ioctl = ll_file_ioctl,
3027         .open      = ll_file_open,
3028         .release        = ll_file_release,
3029         .mmap      = ll_file_mmap,
3030         .llseek  = ll_file_seek,
3031         .splice_read    = ll_file_splice_read,
3032         .fsync    = ll_fsync,
3033         .flush    = ll_flush,
3034         .flock    = ll_file_flock,
3035         .lock      = ll_file_flock
3036 };
3037
3038 /* These are for -o noflock - to return ENOSYS on flock calls */
3039 struct file_operations ll_file_operations_noflock = {
3040         .read      = ll_file_read,
3041         .aio_read    = ll_file_aio_read,
3042         .write    = ll_file_write,
3043         .aio_write   = ll_file_aio_write,
3044         .unlocked_ioctl = ll_file_ioctl,
3045         .open      = ll_file_open,
3046         .release        = ll_file_release,
3047         .mmap      = ll_file_mmap,
3048         .llseek  = ll_file_seek,
3049         .splice_read    = ll_file_splice_read,
3050         .fsync    = ll_fsync,
3051         .flush    = ll_flush,
3052         .flock    = ll_file_noflock,
3053         .lock      = ll_file_noflock
3054 };
3055
3056 struct inode_operations ll_file_inode_operations = {
3057         .setattr        = ll_setattr,
3058         .getattr        = ll_getattr,
3059         .permission     = ll_inode_permission,
3060         .setxattr       = ll_setxattr,
3061         .getxattr       = ll_getxattr,
3062         .listxattr      = ll_listxattr,
3063         .removexattr    = ll_removexattr,
3064         .fiemap         = ll_fiemap,
3065         .get_acl        = ll_get_acl,
3066 };
3067
3068 /* dynamic ioctl number support routins */
3069 static struct llioc_ctl_data {
3070         struct rw_semaphore     ioc_sem;
3071         struct list_head              ioc_head;
3072 } llioc = {
3073         __RWSEM_INITIALIZER(llioc.ioc_sem),
3074         LIST_HEAD_INIT(llioc.ioc_head)
3075 };
3076
3077
3078 struct llioc_data {
3079         struct list_head              iocd_list;
3080         unsigned int        iocd_size;
3081         llioc_callback_t        iocd_cb;
3082         unsigned int        iocd_count;
3083         unsigned int        iocd_cmd[0];
3084 };
3085
3086 void *ll_iocontrol_register(llioc_callback_t cb, int count, unsigned int *cmd)
3087 {
3088         unsigned int size;
3089         struct llioc_data *in_data = NULL;
3090
3091         if (cb == NULL || cmd == NULL ||
3092             count > LLIOC_MAX_CMD || count < 0)
3093                 return NULL;
3094
3095         size = sizeof(*in_data) + count * sizeof(unsigned int);
3096         OBD_ALLOC(in_data, size);
3097         if (in_data == NULL)
3098                 return NULL;
3099
3100         memset(in_data, 0, sizeof(*in_data));
3101         in_data->iocd_size = size;
3102         in_data->iocd_cb = cb;
3103         in_data->iocd_count = count;
3104         memcpy(in_data->iocd_cmd, cmd, sizeof(unsigned int) * count);
3105
3106         down_write(&llioc.ioc_sem);
3107         list_add_tail(&in_data->iocd_list, &llioc.ioc_head);
3108         up_write(&llioc.ioc_sem);
3109
3110         return in_data;
3111 }
3112
3113 void ll_iocontrol_unregister(void *magic)
3114 {
3115         struct llioc_data *tmp;
3116
3117         if (magic == NULL)
3118                 return;
3119
3120         down_write(&llioc.ioc_sem);
3121         list_for_each_entry(tmp, &llioc.ioc_head, iocd_list) {
3122                 if (tmp == magic) {
3123                         unsigned int size = tmp->iocd_size;
3124
3125                         list_del(&tmp->iocd_list);
3126                         up_write(&llioc.ioc_sem);
3127
3128                         OBD_FREE(tmp, size);
3129                         return;
3130                 }
3131         }
3132         up_write(&llioc.ioc_sem);
3133
3134         CWARN("didn't find iocontrol register block with magic: %p\n", magic);
3135 }
3136
3137 EXPORT_SYMBOL(ll_iocontrol_register);
3138 EXPORT_SYMBOL(ll_iocontrol_unregister);
3139
3140 enum llioc_iter ll_iocontrol_call(struct inode *inode, struct file *file,
3141                         unsigned int cmd, unsigned long arg, int *rcp)
3142 {
3143         enum llioc_iter ret = LLIOC_CONT;
3144         struct llioc_data *data;
3145         int rc = -EINVAL, i;
3146
3147         down_read(&llioc.ioc_sem);
3148         list_for_each_entry(data, &llioc.ioc_head, iocd_list) {
3149                 for (i = 0; i < data->iocd_count; i++) {
3150                         if (cmd != data->iocd_cmd[i])
3151                                 continue;
3152
3153                         ret = data->iocd_cb(inode, file, cmd, arg, data, &rc);
3154                         break;
3155                 }
3156
3157                 if (ret == LLIOC_STOP)
3158                         break;
3159         }
3160         up_read(&llioc.ioc_sem);
3161
3162         if (rcp)
3163                 *rcp = rc;
3164         return ret;
3165 }
3166
3167 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
3168 {
3169         struct ll_inode_info *lli = ll_i2info(inode);
3170         struct cl_env_nest nest;
3171         struct lu_env *env;
3172         int result;
3173
3174         if (lli->lli_clob == NULL)
3175                 return 0;
3176
3177         env = cl_env_nested_get(&nest);
3178         if (IS_ERR(env))
3179                 return PTR_ERR(env);
3180
3181         result = cl_conf_set(env, lli->lli_clob, conf);
3182         cl_env_nested_put(&nest, env);
3183
3184         if (conf->coc_opc == OBJECT_CONF_SET) {
3185                 struct ldlm_lock *lock = conf->coc_lock;
3186
3187                 LASSERT(lock != NULL);
3188                 LASSERT(ldlm_has_layout(lock));
3189                 if (result == 0) {
3190                         /* it can only be allowed to match after layout is
3191                          * applied to inode otherwise false layout would be
3192                          * seen. Applying layout shoud happen before dropping
3193                          * the intent lock. */
3194                         ldlm_lock_allow_match(lock);
3195                 }
3196         }
3197         return result;
3198 }
3199
3200 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
3201 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
3202
3203 {
3204         struct ll_sb_info *sbi = ll_i2sbi(inode);
3205         struct obd_capa *oc;
3206         struct ptlrpc_request *req;
3207         struct mdt_body *body;
3208         void *lvbdata;
3209         void *lmm;
3210         int lmmsize;
3211         int rc;
3212
3213         CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
3214                PFID(ll_inode2fid(inode)), !!(lock->l_flags & LDLM_FL_LVB_READY),
3215                lock->l_lvb_data, lock->l_lvb_len);
3216
3217         if ((lock->l_lvb_data != NULL) && (lock->l_flags & LDLM_FL_LVB_READY))
3218                 return 0;
3219
3220         /* if layout lock was granted right away, the layout is returned
3221          * within DLM_LVB of dlm reply; otherwise if the lock was ever
3222          * blocked and then granted via completion ast, we have to fetch
3223          * layout here. Please note that we can't use the LVB buffer in
3224          * completion AST because it doesn't have a large enough buffer */
3225         oc = ll_mdscapa_get(inode);
3226         rc = ll_get_max_mdsize(sbi, &lmmsize);
3227         if (rc == 0)
3228                 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
3229                                 OBD_MD_FLXATTR, XATTR_NAME_LOV, NULL, 0,
3230                                 lmmsize, 0, &req);
3231         capa_put(oc);
3232         if (rc < 0)
3233                 return rc;
3234
3235         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3236         if (body == NULL || body->eadatasize > lmmsize)
3237                 GOTO(out, rc = -EPROTO);
3238
3239         lmmsize = body->eadatasize;
3240         if (lmmsize == 0) /* empty layout */
3241                 GOTO(out, rc = 0);
3242
3243         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
3244         if (lmm == NULL)
3245                 GOTO(out, rc = -EFAULT);
3246
3247         OBD_ALLOC_LARGE(lvbdata, lmmsize);
3248         if (lvbdata == NULL)
3249                 GOTO(out, rc = -ENOMEM);
3250
3251         memcpy(lvbdata, lmm, lmmsize);
3252         lock_res_and_lock(lock);
3253         if (lock->l_lvb_data != NULL)
3254                 OBD_FREE_LARGE(lock->l_lvb_data, lock->l_lvb_len);
3255
3256         lock->l_lvb_data = lvbdata;
3257         lock->l_lvb_len = lmmsize;
3258         unlock_res_and_lock(lock);
3259
3260 out:
3261         ptlrpc_req_finished(req);
3262         return rc;
3263 }
3264
3265 /**
3266  * Apply the layout to the inode. Layout lock is held and will be released
3267  * in this function.
3268  */
3269 static int ll_layout_lock_set(struct lustre_handle *lockh, ldlm_mode_t mode,
3270                                 struct inode *inode, __u32 *gen, bool reconf)
3271 {
3272         struct ll_inode_info *lli = ll_i2info(inode);
3273         struct ll_sb_info    *sbi = ll_i2sbi(inode);
3274         struct ldlm_lock *lock;
3275         struct lustre_md md = { NULL };
3276         struct cl_object_conf conf;
3277         int rc = 0;
3278         bool lvb_ready;
3279         bool wait_layout = false;
3280
3281         LASSERT(lustre_handle_is_used(lockh));
3282
3283         lock = ldlm_handle2lock(lockh);
3284         LASSERT(lock != NULL);
3285         LASSERT(ldlm_has_layout(lock));
3286
3287         LDLM_DEBUG(lock, "File %p/"DFID" being reconfigured: %d.\n",
3288                    inode, PFID(&lli->lli_fid), reconf);
3289
3290         /* in case this is a caching lock and reinstate with new inode */
3291         md_set_lock_data(sbi->ll_md_exp, &lockh->cookie, inode, NULL);
3292
3293         lock_res_and_lock(lock);
3294         lvb_ready = !!(lock->l_flags & LDLM_FL_LVB_READY);
3295         unlock_res_and_lock(lock);
3296         /* checking lvb_ready is racy but this is okay. The worst case is
3297          * that multi processes may configure the file on the same time. */
3298         if (lvb_ready || !reconf) {
3299                 rc = -ENODATA;
3300                 if (lvb_ready) {
3301                         /* layout_gen must be valid if layout lock is not
3302                          * cancelled and stripe has already set */
3303                         *gen = lli->lli_layout_gen;
3304                         rc = 0;
3305                 }
3306                 GOTO(out, rc);
3307         }
3308
3309         rc = ll_layout_fetch(inode, lock);
3310         if (rc < 0)
3311                 GOTO(out, rc);
3312
3313         /* for layout lock, lmm is returned in lock's lvb.
3314          * lvb_data is immutable if the lock is held so it's safe to access it
3315          * without res lock. See the description in ldlm_lock_decref_internal()
3316          * for the condition to free lvb_data of layout lock */
3317         if (lock->l_lvb_data != NULL) {
3318                 rc = obd_unpackmd(sbi->ll_dt_exp, &md.lsm,
3319                                   lock->l_lvb_data, lock->l_lvb_len);
3320                 if (rc >= 0) {
3321                         *gen = LL_LAYOUT_GEN_EMPTY;
3322                         if (md.lsm != NULL)
3323                                 *gen = md.lsm->lsm_layout_gen;
3324                         rc = 0;
3325                 } else {
3326                         CERROR("%s: file "DFID" unpackmd error: %d\n",
3327                                 ll_get_fsname(inode->i_sb, NULL, 0),
3328                                 PFID(&lli->lli_fid), rc);
3329                 }
3330         }
3331         if (rc < 0)
3332                 GOTO(out, rc);
3333
3334         /* set layout to file. Unlikely this will fail as old layout was
3335          * surely eliminated */
3336         memset(&conf, 0, sizeof(conf));
3337         conf.coc_opc = OBJECT_CONF_SET;
3338         conf.coc_inode = inode;
3339         conf.coc_lock = lock;
3340         conf.u.coc_md = &md;
3341         rc = ll_layout_conf(inode, &conf);
3342
3343         if (md.lsm != NULL)
3344                 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
3345
3346         /* refresh layout failed, need to wait */
3347         wait_layout = rc == -EBUSY;
3348
3349 out:
3350         LDLM_LOCK_PUT(lock);
3351         ldlm_lock_decref(lockh, mode);
3352
3353         /* wait for IO to complete if it's still being used. */
3354         if (wait_layout) {
3355                 CDEBUG(D_INODE, "%s: %p/"DFID" wait for layout reconf.\n",
3356                         ll_get_fsname(inode->i_sb, NULL, 0),
3357                         inode, PFID(&lli->lli_fid));
3358
3359                 memset(&conf, 0, sizeof(conf));
3360                 conf.coc_opc = OBJECT_CONF_WAIT;
3361                 conf.coc_inode = inode;
3362                 rc = ll_layout_conf(inode, &conf);
3363                 if (rc == 0)
3364                         rc = -EAGAIN;
3365
3366                 CDEBUG(D_INODE, "file: "DFID" waiting layout return: %d.\n",
3367                         PFID(&lli->lli_fid), rc);
3368         }
3369         return rc;
3370 }
3371
3372 /**
3373  * This function checks if there exists a LAYOUT lock on the client side,
3374  * or enqueues it if it doesn't have one in cache.
3375  *
3376  * This function will not hold layout lock so it may be revoked any time after
3377  * this function returns. Any operations depend on layout should be redone
3378  * in that case.
3379  *
3380  * This function should be called before lov_io_init() to get an uptodate
3381  * layout version, the caller should save the version number and after IO
3382  * is finished, this function should be called again to verify that layout
3383  * is not changed during IO time.
3384  */
3385 int ll_layout_refresh(struct inode *inode, __u32 *gen)
3386 {
3387         struct ll_inode_info  *lli = ll_i2info(inode);
3388         struct ll_sb_info     *sbi = ll_i2sbi(inode);
3389         struct md_op_data     *op_data;
3390         struct lookup_intent   it;
3391         struct lustre_handle   lockh;
3392         ldlm_mode_t            mode;
3393         struct ldlm_enqueue_info einfo = {
3394                 .ei_type = LDLM_IBITS,
3395                 .ei_mode = LCK_CR,
3396                 .ei_cb_bl = ll_md_blocking_ast,
3397                 .ei_cb_cp = ldlm_completion_ast,
3398         };
3399         int rc;
3400
3401         *gen = lli->lli_layout_gen;
3402         if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK))
3403                 return 0;
3404
3405         /* sanity checks */
3406         LASSERT(fid_is_sane(ll_inode2fid(inode)));
3407         LASSERT(S_ISREG(inode->i_mode));
3408
3409         /* mostly layout lock is caching on the local side, so try to match
3410          * it before grabbing layout lock mutex. */
3411         mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0);
3412         if (mode != 0) { /* hit cached lock */
3413                 rc = ll_layout_lock_set(&lockh, mode, inode, gen, false);
3414                 if (rc == 0)
3415                         return 0;
3416
3417                 /* better hold lli_layout_mutex to try again otherwise
3418                  * it will have starvation problem. */
3419         }
3420
3421         /* take layout lock mutex to enqueue layout lock exclusively. */
3422         mutex_lock(&lli->lli_layout_mutex);
3423
3424 again:
3425         /* try again. Maybe somebody else has done this. */
3426         mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0);
3427         if (mode != 0) { /* hit cached lock */
3428                 rc = ll_layout_lock_set(&lockh, mode, inode, gen, true);
3429                 if (rc == -EAGAIN)
3430                         goto again;
3431
3432                 mutex_unlock(&lli->lli_layout_mutex);
3433                 return rc;
3434         }
3435
3436         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
3437                         0, 0, LUSTRE_OPC_ANY, NULL);
3438         if (IS_ERR(op_data)) {
3439                 mutex_unlock(&lli->lli_layout_mutex);
3440                 return PTR_ERR(op_data);
3441         }
3442
3443         /* have to enqueue one */
3444         memset(&it, 0, sizeof(it));
3445         it.it_op = IT_LAYOUT;
3446         lockh.cookie = 0ULL;
3447
3448         LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file %p/"DFID".\n",
3449                         ll_get_fsname(inode->i_sb, NULL, 0), inode,
3450                         PFID(&lli->lli_fid));
3451
3452         rc = md_enqueue(sbi->ll_md_exp, &einfo, &it, op_data, &lockh,
3453                         NULL, 0, NULL, 0);
3454         if (it.d.lustre.it_data != NULL)
3455                 ptlrpc_req_finished(it.d.lustre.it_data);
3456         it.d.lustre.it_data = NULL;
3457
3458         ll_finish_md_op_data(op_data);
3459
3460         mode = it.d.lustre.it_lock_mode;
3461         it.d.lustre.it_lock_mode = 0;
3462         ll_intent_drop_lock(&it);
3463
3464         if (rc == 0) {
3465                 /* set lock data in case this is a new lock */
3466                 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
3467                 rc = ll_layout_lock_set(&lockh, mode, inode, gen, true);
3468                 if (rc == -EAGAIN)
3469                         goto again;
3470         }
3471         mutex_unlock(&lli->lli_layout_mutex);
3472
3473         return rc;
3474 }
3475
3476 /**
3477  *  This function send a restore request to the MDT
3478  */
3479 int ll_layout_restore(struct inode *inode)
3480 {
3481         struct hsm_user_request *hur;
3482         int                      len, rc;
3483
3484         len = sizeof(struct hsm_user_request) +
3485               sizeof(struct hsm_user_item);
3486         OBD_ALLOC(hur, len);
3487         if (hur == NULL)
3488                 return -ENOMEM;
3489
3490         hur->hur_request.hr_action = HUA_RESTORE;
3491         hur->hur_request.hr_archive_id = 0;
3492         hur->hur_request.hr_flags = 0;
3493         memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
3494                sizeof(hur->hur_user_item[0].hui_fid));
3495         hur->hur_user_item[0].hui_extent.length = -1;
3496         hur->hur_request.hr_itemcount = 1;
3497         rc = obd_iocontrol(LL_IOC_HSM_REQUEST, cl_i2sbi(inode)->ll_md_exp,
3498                            len, hur, NULL);
3499         OBD_FREE(hur, len);
3500         return rc;
3501 }