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