Whamcloud - gitweb
LU-12518 llite: proper names/types for offset/pages
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/llite/file.c
33  *
34  * Author: Peter Braam <braam@clusterfs.com>
35  * Author: Phil Schwan <phil@clusterfs.com>
36  * Author: Andreas Dilger <adilger@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <lustre_dlm.h>
41 #include <linux/pagemap.h>
42 #include <linux/file.h>
43 #include <linux/sched.h>
44 #include <linux/user_namespace.h>
45 #include <linux/uidgid.h>
46
47 #include <uapi/linux/lustre/lustre_ioctl.h>
48 #include <lustre_swab.h>
49
50 #include "cl_object.h"
51 #include "llite_internal.h"
52 #include "vvp_internal.h"
53
54 struct split_param {
55         struct inode    *sp_inode;
56         __u16           sp_mirror_id;
57 };
58
59 struct pcc_param {
60         __u64   pa_data_version;
61         __u32   pa_archive_id;
62         __u32   pa_layout_gen;
63 };
64
65 static int
66 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
67
68 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
69                           bool *lease_broken);
70
71 static struct ll_file_data *ll_file_data_get(void)
72 {
73         struct ll_file_data *fd;
74
75         OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, GFP_NOFS);
76         if (fd == NULL)
77                 return NULL;
78
79         fd->fd_write_failed = false;
80         pcc_file_init(&fd->fd_pcc_file);
81
82         return fd;
83 }
84
85 static void ll_file_data_put(struct ll_file_data *fd)
86 {
87         if (fd != NULL)
88                 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
89 }
90
91 /**
92  * Packs all the attributes into @op_data for the CLOSE rpc.
93  */
94 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
95                              struct obd_client_handle *och)
96 {
97         ENTRY;
98
99         ll_prep_md_op_data(op_data, inode, NULL, NULL,
100                            0, 0, LUSTRE_OPC_ANY, NULL);
101
102         op_data->op_attr.ia_mode = inode->i_mode;
103         op_data->op_attr.ia_atime = inode->i_atime;
104         op_data->op_attr.ia_mtime = inode->i_mtime;
105         op_data->op_attr.ia_ctime = inode->i_ctime;
106         op_data->op_attr.ia_size = i_size_read(inode);
107         op_data->op_attr.ia_valid |= (ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
108                                       ATTR_MTIME | ATTR_MTIME_SET |
109                                       ATTR_CTIME);
110         op_data->op_xvalid |= OP_XVALID_CTIME_SET;
111         op_data->op_attr_blocks = inode->i_blocks;
112         op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
113         if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
114                 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
115         op_data->op_open_handle = och->och_open_handle;
116
117         if (och->och_flags & FMODE_WRITE &&
118             ll_file_test_and_clear_flag(ll_i2info(inode), LLIF_DATA_MODIFIED))
119                 /* For HSM: if inode data has been modified, pack it so that
120                  * MDT can set data dirty flag in the archive. */
121                 op_data->op_bias |= MDS_DATA_MODIFIED;
122
123         EXIT;
124 }
125
126 /**
127  * Perform a close, possibly with a bias.
128  * The meaning of "data" depends on the value of "bias".
129  *
130  * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
131  * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
132  * swap layouts with.
133  */
134 static int ll_close_inode_openhandle(struct inode *inode,
135                                      struct obd_client_handle *och,
136                                      enum mds_op_bias bias, void *data)
137 {
138         struct obd_export *md_exp = ll_i2mdexp(inode);
139         const struct ll_inode_info *lli = ll_i2info(inode);
140         struct md_op_data *op_data;
141         struct ptlrpc_request *req = NULL;
142         int rc;
143         ENTRY;
144
145         if (class_exp2obd(md_exp) == NULL) {
146                 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
147                        ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
148                 GOTO(out, rc = 0);
149         }
150
151         OBD_ALLOC_PTR(op_data);
152         /* We leak openhandle and request here on error, but not much to be
153          * done in OOM case since app won't retry close on error either. */
154         if (op_data == NULL)
155                 GOTO(out, rc = -ENOMEM);
156
157         ll_prepare_close(inode, op_data, och);
158         switch (bias) {
159         case MDS_CLOSE_LAYOUT_MERGE:
160                 /* merge blocks from the victim inode */
161                 op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
162                 op_data->op_attr.ia_valid |= ATTR_SIZE;
163                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
164                 /* fallthrough */
165         case MDS_CLOSE_LAYOUT_SPLIT:
166         case MDS_CLOSE_LAYOUT_SWAP: {
167                 struct split_param *sp = data;
168
169                 LASSERT(data != NULL);
170                 op_data->op_bias |= bias;
171                 op_data->op_data_version = 0;
172                 op_data->op_lease_handle = och->och_lease_handle;
173                 if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
174                         op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
175                         op_data->op_mirror_id = sp->sp_mirror_id;
176                 } else {
177                         op_data->op_fid2 = *ll_inode2fid(data);
178                 }
179                 break;
180         }
181
182         case MDS_CLOSE_RESYNC_DONE: {
183                 struct ll_ioc_lease *ioc = data;
184
185                 LASSERT(data != NULL);
186                 op_data->op_attr_blocks +=
187                         ioc->lil_count * op_data->op_attr_blocks;
188                 op_data->op_attr.ia_valid |= ATTR_SIZE;
189                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
190                 op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
191
192                 op_data->op_lease_handle = och->och_lease_handle;
193                 op_data->op_data = &ioc->lil_ids[0];
194                 op_data->op_data_size =
195                         ioc->lil_count * sizeof(ioc->lil_ids[0]);
196                 break;
197         }
198
199         case MDS_PCC_ATTACH: {
200                 struct pcc_param *param = data;
201
202                 LASSERT(data != NULL);
203                 op_data->op_bias |= MDS_HSM_RELEASE | MDS_PCC_ATTACH;
204                 op_data->op_archive_id = param->pa_archive_id;
205                 op_data->op_data_version = param->pa_data_version;
206                 op_data->op_lease_handle = och->och_lease_handle;
207                 break;
208         }
209
210         case MDS_HSM_RELEASE:
211                 LASSERT(data != NULL);
212                 op_data->op_bias |= MDS_HSM_RELEASE;
213                 op_data->op_data_version = *(__u64 *)data;
214                 op_data->op_lease_handle = och->och_lease_handle;
215                 op_data->op_attr.ia_valid |= ATTR_SIZE;
216                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
217                 break;
218
219         default:
220                 LASSERT(data == NULL);
221                 break;
222         }
223
224         if (!(op_data->op_attr.ia_valid & ATTR_SIZE))
225                 op_data->op_xvalid |= OP_XVALID_LAZYSIZE;
226         if (!(op_data->op_xvalid & OP_XVALID_BLOCKS))
227                 op_data->op_xvalid |= OP_XVALID_LAZYBLOCKS;
228
229         rc = md_close(md_exp, op_data, och->och_mod, &req);
230         if (rc != 0 && rc != -EINTR)
231                 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
232                        md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
233
234         if (rc == 0 && op_data->op_bias & bias) {
235                 struct mdt_body *body;
236
237                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
238                 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
239                         rc = -EBUSY;
240
241                 if (bias & MDS_PCC_ATTACH) {
242                         struct pcc_param *param = data;
243
244                         param->pa_layout_gen = body->mbo_layout_gen;
245                 }
246         }
247
248         ll_finish_md_op_data(op_data);
249         EXIT;
250 out:
251
252         md_clear_open_replay_data(md_exp, och);
253         och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
254         OBD_FREE_PTR(och);
255
256         ptlrpc_req_finished(req);       /* This is close request */
257         return rc;
258 }
259
260 int ll_md_real_close(struct inode *inode, fmode_t fmode)
261 {
262         struct ll_inode_info *lli = ll_i2info(inode);
263         struct obd_client_handle **och_p;
264         struct obd_client_handle *och;
265         __u64 *och_usecount;
266         int rc = 0;
267         ENTRY;
268
269         if (fmode & FMODE_WRITE) {
270                 och_p = &lli->lli_mds_write_och;
271                 och_usecount = &lli->lli_open_fd_write_count;
272         } else if (fmode & FMODE_EXEC) {
273                 och_p = &lli->lli_mds_exec_och;
274                 och_usecount = &lli->lli_open_fd_exec_count;
275         } else {
276                 LASSERT(fmode & FMODE_READ);
277                 och_p = &lli->lli_mds_read_och;
278                 och_usecount = &lli->lli_open_fd_read_count;
279         }
280
281         mutex_lock(&lli->lli_och_mutex);
282         if (*och_usecount > 0) {
283                 /* There are still users of this handle, so skip
284                  * freeing it. */
285                 mutex_unlock(&lli->lli_och_mutex);
286                 RETURN(0);
287         }
288
289         och = *och_p;
290         *och_p = NULL;
291         mutex_unlock(&lli->lli_och_mutex);
292
293         if (och != NULL) {
294                 /* There might be a race and this handle may already
295                  * be closed. */
296                 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
297         }
298
299         RETURN(rc);
300 }
301
302 static int ll_md_close(struct inode *inode, struct file *file)
303 {
304         union ldlm_policy_data policy = {
305                 .l_inodebits    = { MDS_INODELOCK_OPEN },
306         };
307         __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
308         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
309         struct ll_inode_info *lli = ll_i2info(inode);
310         struct lustre_handle lockh;
311         enum ldlm_mode lockmode;
312         int rc = 0;
313         ENTRY;
314
315         /* clear group lock, if present */
316         if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
317                 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
318
319         if (fd->fd_lease_och != NULL) {
320                 bool lease_broken;
321
322                 /* Usually the lease is not released when the
323                  * application crashed, we need to release here. */
324                 rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken);
325                 CDEBUG(rc ? D_ERROR : D_INODE, "Clean up lease "DFID" %d/%d\n",
326                         PFID(&lli->lli_fid), rc, lease_broken);
327
328                 fd->fd_lease_och = NULL;
329         }
330
331         if (fd->fd_och != NULL) {
332                 rc = ll_close_inode_openhandle(inode, fd->fd_och, 0, NULL);
333                 fd->fd_och = NULL;
334                 GOTO(out, rc);
335         }
336
337         /* Let's see if we have good enough OPEN lock on the file and if
338            we can skip talking to MDS */
339         mutex_lock(&lli->lli_och_mutex);
340         if (fd->fd_omode & FMODE_WRITE) {
341                 lockmode = LCK_CW;
342                 LASSERT(lli->lli_open_fd_write_count);
343                 lli->lli_open_fd_write_count--;
344         } else if (fd->fd_omode & FMODE_EXEC) {
345                 lockmode = LCK_PR;
346                 LASSERT(lli->lli_open_fd_exec_count);
347                 lli->lli_open_fd_exec_count--;
348         } else {
349                 lockmode = LCK_CR;
350                 LASSERT(lli->lli_open_fd_read_count);
351                 lli->lli_open_fd_read_count--;
352         }
353         mutex_unlock(&lli->lli_och_mutex);
354
355         /* LU-4398: do not cache write open lock if the file has exec bit */
356         if ((lockmode == LCK_CW && inode->i_mode & S_IXUGO) ||
357             !md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
358                            LDLM_IBITS, &policy, lockmode, &lockh))
359                 rc = ll_md_real_close(inode, fd->fd_omode);
360
361 out:
362         LUSTRE_FPRIVATE(file) = NULL;
363         ll_file_data_put(fd);
364
365         RETURN(rc);
366 }
367
368 /* While this returns an error code, fput() the caller does not, so we need
369  * to make every effort to clean up all of our state here.  Also, applications
370  * rarely check close errors and even if an error is returned they will not
371  * re-try the close call.
372  */
373 int ll_file_release(struct inode *inode, struct file *file)
374 {
375         struct ll_file_data *fd;
376         struct ll_sb_info *sbi = ll_i2sbi(inode);
377         struct ll_inode_info *lli = ll_i2info(inode);
378         ktime_t kstart = ktime_get();
379         int rc;
380
381         ENTRY;
382
383         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
384                PFID(ll_inode2fid(inode)), inode);
385
386         fd = LUSTRE_FPRIVATE(file);
387         LASSERT(fd != NULL);
388
389         /* The last ref on @file, maybe not the the owner pid of statahead,
390          * because parent and child process can share the same file handle. */
391         if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
392                 ll_deauthorize_statahead(inode, fd);
393
394         if (inode->i_sb->s_root == file_dentry(file)) {
395                 LUSTRE_FPRIVATE(file) = NULL;
396                 ll_file_data_put(fd);
397                 GOTO(out, rc = 0);
398         }
399
400         pcc_file_release(inode, file);
401
402         if (!S_ISDIR(inode->i_mode)) {
403                 if (lli->lli_clob != NULL)
404                         lov_read_and_clear_async_rc(lli->lli_clob);
405                 lli->lli_async_rc = 0;
406         }
407
408         rc = ll_md_close(inode, file);
409
410         if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
411                 libcfs_debug_dumplog();
412
413 out:
414         if (!rc && inode->i_sb->s_root != file_dentry(file))
415                 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
416                                    ktime_us_delta(ktime_get(), kstart));
417         RETURN(rc);
418 }
419
420 static inline int ll_dom_readpage(void *data, struct page *page)
421 {
422         struct niobuf_local *lnb = data;
423         void *kaddr;
424
425         kaddr = ll_kmap_atomic(page, KM_USER0);
426         memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
427         if (lnb->lnb_len < PAGE_SIZE)
428                 memset(kaddr + lnb->lnb_len, 0,
429                        PAGE_SIZE - lnb->lnb_len);
430         flush_dcache_page(page);
431         SetPageUptodate(page);
432         ll_kunmap_atomic(kaddr, KM_USER0);
433         unlock_page(page);
434
435         return 0;
436 }
437
438 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req,
439                         struct lookup_intent *it)
440 {
441         struct ll_inode_info *lli = ll_i2info(inode);
442         struct cl_object *obj = lli->lli_clob;
443         struct address_space *mapping = inode->i_mapping;
444         struct page *vmpage;
445         struct niobuf_remote *rnb;
446         struct mdt_body *body;
447         char *data;
448         unsigned long index, start;
449         struct niobuf_local lnb;
450
451         ENTRY;
452
453         if (obj == NULL)
454                 RETURN_EXIT;
455
456         if (!req_capsule_field_present(&req->rq_pill, &RMF_NIOBUF_INLINE,
457                                        RCL_SERVER))
458                 RETURN_EXIT;
459
460         rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
461         if (rnb == NULL || rnb->rnb_len == 0)
462                 RETURN_EXIT;
463
464         /* LU-11595: Server may return whole file and that is OK always or
465          * it may return just file tail and its offset must be aligned with
466          * client PAGE_SIZE to be used on that client, if server's PAGE_SIZE is
467          * smaller then offset may be not aligned and that data is just ignored.
468          */
469         if (rnb->rnb_offset & ~PAGE_MASK)
470                 RETURN_EXIT;
471
472         /* Server returns whole file or just file tail if it fills in reply
473          * buffer, in both cases total size should be equal to the file size.
474          */
475         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
476         if (rnb->rnb_offset + rnb->rnb_len != body->mbo_dom_size) {
477                 CERROR("%s: server returns off/len %llu/%u but size %llu\n",
478                        ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
479                        rnb->rnb_len, body->mbo_dom_size);
480                 RETURN_EXIT;
481         }
482
483         CDEBUG(D_INFO, "Get data along with open at %llu len %i, size %llu\n",
484                rnb->rnb_offset, rnb->rnb_len, body->mbo_dom_size);
485
486         data = (char *)rnb + sizeof(*rnb);
487
488         lnb.lnb_file_offset = rnb->rnb_offset;
489         start = lnb.lnb_file_offset >> PAGE_SHIFT;
490         index = 0;
491         LASSERT((lnb.lnb_file_offset & ~PAGE_MASK) == 0);
492         lnb.lnb_page_offset = 0;
493         do {
494                 lnb.lnb_data = data + (index << PAGE_SHIFT);
495                 lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
496                 if (lnb.lnb_len > PAGE_SIZE)
497                         lnb.lnb_len = PAGE_SIZE;
498
499                 vmpage = read_cache_page(mapping, index + start,
500                                          ll_dom_readpage, &lnb);
501                 if (IS_ERR(vmpage)) {
502                         CWARN("%s: cannot fill page %lu for "DFID
503                               " with data: rc = %li\n",
504                               ll_i2sbi(inode)->ll_fsname, index + start,
505                               PFID(lu_object_fid(&obj->co_lu)),
506                               PTR_ERR(vmpage));
507                         break;
508                 }
509                 put_page(vmpage);
510                 index++;
511         } while (rnb->rnb_len > (index << PAGE_SHIFT));
512         EXIT;
513 }
514
515 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
516                                 struct lookup_intent *itp)
517 {
518         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
519         struct dentry *parent = de->d_parent;
520         char *name = NULL;
521         int len = 0;
522         struct md_op_data *op_data;
523         struct ptlrpc_request *req = NULL;
524         int rc;
525         ENTRY;
526
527         LASSERT(parent != NULL);
528         LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
529
530         /* if server supports open-by-fid, or file name is invalid, don't pack
531          * name in open request */
532         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
533             !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
534 retry:
535                 len = de->d_name.len;
536                 name = kmalloc(len + 1, GFP_NOFS);
537                 if (!name)
538                         RETURN(-ENOMEM);
539
540                 /* race here */
541                 spin_lock(&de->d_lock);
542                 if (len != de->d_name.len) {
543                         spin_unlock(&de->d_lock);
544                         kfree(name);
545                         goto retry;
546                 }
547                 memcpy(name, de->d_name.name, len);
548                 name[len] = '\0';
549                 spin_unlock(&de->d_lock);
550
551                 if (!lu_name_is_valid_2(name, len)) {
552                         kfree(name);
553                         RETURN(-ESTALE);
554                 }
555         }
556
557         op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
558                                      name, len, 0, LUSTRE_OPC_ANY, NULL);
559         if (IS_ERR(op_data)) {
560                 kfree(name);
561                 RETURN(PTR_ERR(op_data));
562         }
563         op_data->op_data = lmm;
564         op_data->op_data_size = lmmsize;
565
566         rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
567                             &ll_md_blocking_ast, 0);
568         kfree(name);
569         ll_finish_md_op_data(op_data);
570         if (rc == -ESTALE) {
571                 /* reason for keep own exit path - don`t flood log
572                  * with messages with -ESTALE errors.
573                  */
574                 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
575                      it_open_error(DISP_OPEN_OPEN, itp))
576                         GOTO(out, rc);
577                 ll_release_openhandle(de, itp);
578                 GOTO(out, rc);
579         }
580
581         if (it_disposition(itp, DISP_LOOKUP_NEG))
582                 GOTO(out, rc = -ENOENT);
583
584         if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
585                 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
586                 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
587                 GOTO(out, rc);
588         }
589
590         rc = ll_prep_inode(&de->d_inode, req, NULL, itp);
591
592         if (!rc && itp->it_lock_mode) {
593                 struct lustre_handle handle = {.cookie = itp->it_lock_handle};
594                 struct ldlm_lock *lock;
595                 bool has_dom_bit = false;
596
597                 /* If we got a lock back and it has a LOOKUP bit set,
598                  * make sure the dentry is marked as valid so we can find it.
599                  * We don't need to care about actual hashing since other bits
600                  * of kernel will deal with that later.
601                  */
602                 lock = ldlm_handle2lock(&handle);
603                 if (lock) {
604                         has_dom_bit = ldlm_has_dom(lock);
605                         if (lock->l_policy_data.l_inodebits.bits &
606                             MDS_INODELOCK_LOOKUP)
607                                 d_lustre_revalidate(de);
608
609                         LDLM_LOCK_PUT(lock);
610                 }
611                 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, NULL);
612                 if (has_dom_bit)
613                         ll_dom_finish_open(de->d_inode, req, itp);
614         }
615
616 out:
617         ptlrpc_req_finished(req);
618         ll_intent_drop_lock(itp);
619
620         /* We did open by fid, but by the time we got to the server,
621          * the object disappeared. If this is a create, we cannot really
622          * tell the userspace that the file it was trying to create
623          * does not exist. Instead let's return -ESTALE, and the VFS will
624          * retry the create with LOOKUP_REVAL that we are going to catch
625          * in ll_revalidate_dentry() and use lookup then.
626          */
627         if (rc == -ENOENT && itp->it_op & IT_CREAT)
628                 rc = -ESTALE;
629
630         RETURN(rc);
631 }
632
633 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
634                        struct obd_client_handle *och)
635 {
636         struct mdt_body *body;
637
638         body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
639         och->och_open_handle = body->mbo_open_handle;
640         och->och_fid = body->mbo_fid1;
641         och->och_lease_handle.cookie = it->it_lock_handle;
642         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
643         och->och_flags = it->it_flags;
644
645         return md_set_open_replay_data(md_exp, och, it);
646 }
647
648 static int ll_local_open(struct file *file, struct lookup_intent *it,
649                          struct ll_file_data *fd, struct obd_client_handle *och)
650 {
651         struct inode *inode = file_inode(file);
652         ENTRY;
653
654         LASSERT(!LUSTRE_FPRIVATE(file));
655
656         LASSERT(fd != NULL);
657
658         if (och) {
659                 int rc;
660
661                 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
662                 if (rc != 0)
663                         RETURN(rc);
664         }
665
666         LUSTRE_FPRIVATE(file) = fd;
667         ll_readahead_init(inode, &fd->fd_ras);
668         fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
669
670         /* ll_cl_context initialize */
671         rwlock_init(&fd->fd_lock);
672         INIT_LIST_HEAD(&fd->fd_lccs);
673
674         RETURN(0);
675 }
676
677 /* Open a file, and (for the very first open) create objects on the OSTs at
678  * this time.  If opened with O_LOV_DELAY_CREATE, then we don't do the object
679  * creation or open until ll_lov_setstripe() ioctl is called.
680  *
681  * If we already have the stripe MD locally then we don't request it in
682  * md_open(), by passing a lmm_size = 0.
683  *
684  * It is up to the application to ensure no other processes open this file
685  * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
686  * used.  We might be able to avoid races of that sort by getting lli_open_sem
687  * before returning in the O_LOV_DELAY_CREATE case and dropping it here
688  * or in ll_file_release(), but I'm not sure that is desirable/necessary.
689  */
690 int ll_file_open(struct inode *inode, struct file *file)
691 {
692         struct ll_inode_info *lli = ll_i2info(inode);
693         struct lookup_intent *it, oit = { .it_op = IT_OPEN,
694                                           .it_flags = file->f_flags };
695         struct obd_client_handle **och_p = NULL;
696         __u64 *och_usecount = NULL;
697         struct ll_file_data *fd;
698         ktime_t kstart = ktime_get();
699         int rc = 0;
700         ENTRY;
701
702         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
703                PFID(ll_inode2fid(inode)), inode, file->f_flags);
704
705         it = file->private_data; /* XXX: compat macro */
706         file->private_data = NULL; /* prevent ll_local_open assertion */
707
708         fd = ll_file_data_get();
709         if (fd == NULL)
710                 GOTO(out_nofiledata, rc = -ENOMEM);
711
712         fd->fd_file = file;
713         if (S_ISDIR(inode->i_mode))
714                 ll_authorize_statahead(inode, fd);
715
716         if (inode->i_sb->s_root == file_dentry(file)) {
717                 LUSTRE_FPRIVATE(file) = fd;
718                 RETURN(0);
719         }
720
721         if (!it || !it->it_disposition) {
722                 /* Convert f_flags into access mode. We cannot use file->f_mode,
723                  * because everything but O_ACCMODE mask was stripped from
724                  * there */
725                 if ((oit.it_flags + 1) & O_ACCMODE)
726                         oit.it_flags++;
727                 if (file->f_flags & O_TRUNC)
728                         oit.it_flags |= FMODE_WRITE;
729
730                 /* kernel only call f_op->open in dentry_open.  filp_open calls
731                  * dentry_open after call to open_namei that checks permissions.
732                  * Only nfsd_open call dentry_open directly without checking
733                  * permissions and because of that this code below is safe.
734                  */
735                 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
736                         oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
737
738                 /* We do not want O_EXCL here, presumably we opened the file
739                  * already? XXX - NFS implications? */
740                 oit.it_flags &= ~O_EXCL;
741
742                 /* bug20584, if "it_flags" contains O_CREAT, the file will be
743                  * created if necessary, then "IT_CREAT" should be set to keep
744                  * consistent with it */
745                 if (oit.it_flags & O_CREAT)
746                         oit.it_op |= IT_CREAT;
747
748                 it = &oit;
749         }
750
751 restart:
752         /* Let's see if we have file open on MDS already. */
753         if (it->it_flags & FMODE_WRITE) {
754                 och_p = &lli->lli_mds_write_och;
755                 och_usecount = &lli->lli_open_fd_write_count;
756         } else if (it->it_flags & FMODE_EXEC) {
757                 och_p = &lli->lli_mds_exec_och;
758                 och_usecount = &lli->lli_open_fd_exec_count;
759          } else {
760                 och_p = &lli->lli_mds_read_och;
761                 och_usecount = &lli->lli_open_fd_read_count;
762         }
763
764         mutex_lock(&lli->lli_och_mutex);
765         if (*och_p) { /* Open handle is present */
766                 if (it_disposition(it, DISP_OPEN_OPEN)) {
767                         /* Well, there's extra open request that we do not need,
768                            let's close it somehow. This will decref request. */
769                         rc = it_open_error(DISP_OPEN_OPEN, it);
770                         if (rc) {
771                                 mutex_unlock(&lli->lli_och_mutex);
772                                 GOTO(out_openerr, rc);
773                         }
774
775                         ll_release_openhandle(file_dentry(file), it);
776                 }
777                 (*och_usecount)++;
778
779                 rc = ll_local_open(file, it, fd, NULL);
780                 if (rc) {
781                         (*och_usecount)--;
782                         mutex_unlock(&lli->lli_och_mutex);
783                         GOTO(out_openerr, rc);
784                 }
785         } else {
786                 LASSERT(*och_usecount == 0);
787                 if (!it->it_disposition) {
788                         struct dentry *dentry = file_dentry(file);
789                         struct ll_dentry_data *ldd;
790
791                         /* We cannot just request lock handle now, new ELC code
792                            means that one of other OPEN locks for this file
793                            could be cancelled, and since blocking ast handler
794                            would attempt to grab och_mutex as well, that would
795                            result in a deadlock */
796                         mutex_unlock(&lli->lli_och_mutex);
797                         /*
798                          * Normally called under two situations:
799                          * 1. NFS export.
800                          * 2. A race/condition on MDS resulting in no open
801                          *    handle to be returned from LOOKUP|OPEN request,
802                          *    for example if the target entry was a symlink.
803                          *
804                          *  Only fetch MDS_OPEN_LOCK if this is in NFS path,
805                          *  marked by a bit set in ll_iget_for_nfs. Clear the
806                          *  bit so that it's not confusing later callers.
807                          *
808                          *  NB; when ldd is NULL, it must have come via normal
809                          *  lookup path only, since ll_iget_for_nfs always calls
810                          *  ll_d_init().
811                          */
812                         ldd = ll_d2d(dentry);
813                         if (ldd && ldd->lld_nfs_dentry) {
814                                 ldd->lld_nfs_dentry = 0;
815                                 if (!filename_is_volatile(dentry->d_name.name,
816                                                           dentry->d_name.len,
817                                                           NULL))
818                                         it->it_flags |= MDS_OPEN_LOCK;
819                         }
820
821                         /*
822                          * Always specify MDS_OPEN_BY_FID because we don't want
823                          * to get file with different fid.
824                          */
825                         it->it_flags |= MDS_OPEN_BY_FID;
826                         rc = ll_intent_file_open(dentry, NULL, 0, it);
827                         if (rc)
828                                 GOTO(out_openerr, rc);
829
830                         goto restart;
831                 }
832                 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
833                 if (!*och_p)
834                         GOTO(out_och_free, rc = -ENOMEM);
835
836                 (*och_usecount)++;
837
838                 /* md_intent_lock() didn't get a request ref if there was an
839                  * open error, so don't do cleanup on the request here
840                  * (bug 3430) */
841                 /* XXX (green): Should not we bail out on any error here, not
842                  * just open error? */
843                 rc = it_open_error(DISP_OPEN_OPEN, it);
844                 if (rc != 0)
845                         GOTO(out_och_free, rc);
846
847                 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
848                          "inode %p: disposition %x, status %d\n", inode,
849                          it_disposition(it, ~0), it->it_status);
850
851                 rc = ll_local_open(file, it, fd, *och_p);
852                 if (rc)
853                         GOTO(out_och_free, rc);
854         }
855
856         rc = pcc_file_open(inode, file);
857         if (rc)
858                 GOTO(out_och_free, rc);
859
860         mutex_unlock(&lli->lli_och_mutex);
861         fd = NULL;
862
863         /* Must do this outside lli_och_mutex lock to prevent deadlock where
864            different kind of OPEN lock for this same inode gets cancelled
865            by ldlm_cancel_lru */
866         if (!S_ISREG(inode->i_mode))
867                 GOTO(out_och_free, rc);
868
869         cl_lov_delay_create_clear(&file->f_flags);
870         GOTO(out_och_free, rc);
871
872 out_och_free:
873         if (rc) {
874                 if (och_p && *och_p) {
875                         OBD_FREE(*och_p, sizeof (struct obd_client_handle));
876                         *och_p = NULL; /* OBD_FREE writes some magic there */
877                         (*och_usecount)--;
878                 }
879                 mutex_unlock(&lli->lli_och_mutex);
880
881 out_openerr:
882                 if (lli->lli_opendir_key == fd)
883                         ll_deauthorize_statahead(inode, fd);
884
885                 if (fd != NULL)
886                         ll_file_data_put(fd);
887         } else {
888                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
889                                    ktime_us_delta(ktime_get(), kstart));
890         }
891
892 out_nofiledata:
893         if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
894                 ptlrpc_req_finished(it->it_request);
895                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
896         }
897
898         return rc;
899 }
900
901 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
902                         struct ldlm_lock_desc *desc, void *data, int flag)
903 {
904         int rc;
905         struct lustre_handle lockh;
906         ENTRY;
907
908         switch (flag) {
909         case LDLM_CB_BLOCKING:
910                 ldlm_lock2handle(lock, &lockh);
911                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
912                 if (rc < 0) {
913                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
914                         RETURN(rc);
915                 }
916                 break;
917         case LDLM_CB_CANCELING:
918                 /* do nothing */
919                 break;
920         }
921         RETURN(0);
922 }
923
924 /**
925  * When setting a lease on a file, we take ownership of the lli_mds_*_och
926  * and save it as fd->fd_och so as to force client to reopen the file even
927  * if it has an open lock in cache already.
928  */
929 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
930                                 struct lustre_handle *old_open_handle)
931 {
932         struct ll_inode_info *lli = ll_i2info(inode);
933         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
934         struct obd_client_handle **och_p;
935         __u64 *och_usecount;
936         int rc = 0;
937         ENTRY;
938
939         /* Get the openhandle of the file */
940         mutex_lock(&lli->lli_och_mutex);
941         if (fd->fd_lease_och != NULL)
942                 GOTO(out_unlock, rc = -EBUSY);
943
944         if (fd->fd_och == NULL) {
945                 if (file->f_mode & FMODE_WRITE) {
946                         LASSERT(lli->lli_mds_write_och != NULL);
947                         och_p = &lli->lli_mds_write_och;
948                         och_usecount = &lli->lli_open_fd_write_count;
949                 } else {
950                         LASSERT(lli->lli_mds_read_och != NULL);
951                         och_p = &lli->lli_mds_read_och;
952                         och_usecount = &lli->lli_open_fd_read_count;
953                 }
954
955                 if (*och_usecount > 1)
956                         GOTO(out_unlock, rc = -EBUSY);
957
958                 fd->fd_och = *och_p;
959                 *och_usecount = 0;
960                 *och_p = NULL;
961         }
962
963         *old_open_handle = fd->fd_och->och_open_handle;
964
965         EXIT;
966 out_unlock:
967         mutex_unlock(&lli->lli_och_mutex);
968         return rc;
969 }
970
971 /**
972  * Release ownership on lli_mds_*_och when putting back a file lease.
973  */
974 static int ll_lease_och_release(struct inode *inode, struct file *file)
975 {
976         struct ll_inode_info *lli = ll_i2info(inode);
977         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
978         struct obd_client_handle **och_p;
979         struct obd_client_handle *old_och = NULL;
980         __u64 *och_usecount;
981         int rc = 0;
982         ENTRY;
983
984         mutex_lock(&lli->lli_och_mutex);
985         if (file->f_mode & FMODE_WRITE) {
986                 och_p = &lli->lli_mds_write_och;
987                 och_usecount = &lli->lli_open_fd_write_count;
988         } else {
989                 och_p = &lli->lli_mds_read_och;
990                 och_usecount = &lli->lli_open_fd_read_count;
991         }
992
993         /* The file may have been open by another process (broken lease) so
994          * *och_p is not NULL. In this case we should simply increase usecount
995          * and close fd_och.
996          */
997         if (*och_p != NULL) {
998                 old_och = fd->fd_och;
999                 (*och_usecount)++;
1000         } else {
1001                 *och_p = fd->fd_och;
1002                 *och_usecount = 1;
1003         }
1004         fd->fd_och = NULL;
1005         mutex_unlock(&lli->lli_och_mutex);
1006
1007         if (old_och != NULL)
1008                 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1009
1010         RETURN(rc);
1011 }
1012
1013 /**
1014  * Acquire a lease and open the file.
1015  */
1016 static struct obd_client_handle *
1017 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1018               __u64 open_flags)
1019 {
1020         struct lookup_intent it = { .it_op = IT_OPEN };
1021         struct ll_sb_info *sbi = ll_i2sbi(inode);
1022         struct md_op_data *op_data;
1023         struct ptlrpc_request *req = NULL;
1024         struct lustre_handle old_open_handle = { 0 };
1025         struct obd_client_handle *och = NULL;
1026         int rc;
1027         int rc2;
1028         ENTRY;
1029
1030         if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1031                 RETURN(ERR_PTR(-EINVAL));
1032
1033         if (file != NULL) {
1034                 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1035                         RETURN(ERR_PTR(-EPERM));
1036
1037                 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1038                 if (rc)
1039                         RETURN(ERR_PTR(rc));
1040         }
1041
1042         OBD_ALLOC_PTR(och);
1043         if (och == NULL)
1044                 RETURN(ERR_PTR(-ENOMEM));
1045
1046         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1047                                         LUSTRE_OPC_ANY, NULL);
1048         if (IS_ERR(op_data))
1049                 GOTO(out, rc = PTR_ERR(op_data));
1050
1051         /* To tell the MDT this openhandle is from the same owner */
1052         op_data->op_open_handle = old_open_handle;
1053
1054         it.it_flags = fmode | open_flags;
1055         it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1056         rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1057                             &ll_md_blocking_lease_ast,
1058         /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1059          * it can be cancelled which may mislead applications that the lease is
1060          * broken;
1061          * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1062          * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1063          * doesn't deal with openhandle, so normal openhandle will be leaked. */
1064                             LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1065         ll_finish_md_op_data(op_data);
1066         ptlrpc_req_finished(req);
1067         if (rc < 0)
1068                 GOTO(out_release_it, rc);
1069
1070         if (it_disposition(&it, DISP_LOOKUP_NEG))
1071                 GOTO(out_release_it, rc = -ENOENT);
1072
1073         rc = it_open_error(DISP_OPEN_OPEN, &it);
1074         if (rc)
1075                 GOTO(out_release_it, rc);
1076
1077         LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1078         rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1079         if (rc)
1080                 GOTO(out_release_it, rc);
1081
1082         if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1083                 GOTO(out_close, rc = -EOPNOTSUPP);
1084
1085         /* already get lease, handle lease lock */
1086         ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1087         if (it.it_lock_mode == 0 ||
1088             it.it_lock_bits != MDS_INODELOCK_OPEN) {
1089                 /* open lock must return for lease */
1090                 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1091                         PFID(ll_inode2fid(inode)), it.it_lock_mode,
1092                         it.it_lock_bits);
1093                 GOTO(out_close, rc = -EPROTO);
1094         }
1095
1096         ll_intent_release(&it);
1097         RETURN(och);
1098
1099 out_close:
1100         /* Cancel open lock */
1101         if (it.it_lock_mode != 0) {
1102                 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1103                                             it.it_lock_mode);
1104                 it.it_lock_mode = 0;
1105                 och->och_lease_handle.cookie = 0ULL;
1106         }
1107         rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1108         if (rc2 < 0)
1109                 CERROR("%s: error closing file "DFID": %d\n",
1110                        sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1111         och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1112 out_release_it:
1113         ll_intent_release(&it);
1114 out:
1115         if (och != NULL)
1116                 OBD_FREE_PTR(och);
1117         RETURN(ERR_PTR(rc));
1118 }
1119
1120 /**
1121  * Check whether a layout swap can be done between two inodes.
1122  *
1123  * \param[in] inode1  First inode to check
1124  * \param[in] inode2  Second inode to check
1125  *
1126  * \retval 0 on success, layout swap can be performed between both inodes
1127  * \retval negative error code if requirements are not met
1128  */
1129 static int ll_check_swap_layouts_validity(struct inode *inode1,
1130                                           struct inode *inode2)
1131 {
1132         if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1133                 return -EINVAL;
1134
1135         if (inode_permission(inode1, MAY_WRITE) ||
1136             inode_permission(inode2, MAY_WRITE))
1137                 return -EPERM;
1138
1139         if (inode1->i_sb != inode2->i_sb)
1140                 return -EXDEV;
1141
1142         return 0;
1143 }
1144
1145 static int ll_swap_layouts_close(struct obd_client_handle *och,
1146                                  struct inode *inode, struct inode *inode2)
1147 {
1148         const struct lu_fid     *fid1 = ll_inode2fid(inode);
1149         const struct lu_fid     *fid2;
1150         int                      rc;
1151         ENTRY;
1152
1153         CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1154                ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1155
1156         rc = ll_check_swap_layouts_validity(inode, inode2);
1157         if (rc < 0)
1158                 GOTO(out_free_och, rc);
1159
1160         /* We now know that inode2 is a lustre inode */
1161         fid2 = ll_inode2fid(inode2);
1162
1163         rc = lu_fid_cmp(fid1, fid2);
1164         if (rc == 0)
1165                 GOTO(out_free_och, rc = -EINVAL);
1166
1167         /* Close the file and {swap,merge} layouts between inode & inode2.
1168          * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1169          * because we still need it to pack l_remote_handle to MDT. */
1170         rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1171                                        inode2);
1172
1173         och = NULL; /* freed in ll_close_inode_openhandle() */
1174
1175 out_free_och:
1176         if (och != NULL)
1177                 OBD_FREE_PTR(och);
1178
1179         RETURN(rc);
1180 }
1181
1182 /**
1183  * Release lease and close the file.
1184  * It will check if the lease has ever broken.
1185  */
1186 static int ll_lease_close_intent(struct obd_client_handle *och,
1187                                  struct inode *inode,
1188                                  bool *lease_broken, enum mds_op_bias bias,
1189                                  void *data)
1190 {
1191         struct ldlm_lock *lock;
1192         bool cancelled = true;
1193         int rc;
1194         ENTRY;
1195
1196         lock = ldlm_handle2lock(&och->och_lease_handle);
1197         if (lock != NULL) {
1198                 lock_res_and_lock(lock);
1199                 cancelled = ldlm_is_cancel(lock);
1200                 unlock_res_and_lock(lock);
1201                 LDLM_LOCK_PUT(lock);
1202         }
1203
1204         CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1205                PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1206
1207         if (lease_broken != NULL)
1208                 *lease_broken = cancelled;
1209
1210         if (!cancelled && !bias)
1211                 ldlm_cli_cancel(&och->och_lease_handle, 0);
1212
1213         if (cancelled) { /* no need to excute intent */
1214                 bias = 0;
1215                 data = NULL;
1216         }
1217
1218         rc = ll_close_inode_openhandle(inode, och, bias, data);
1219         RETURN(rc);
1220 }
1221
1222 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1223                           bool *lease_broken)
1224 {
1225         return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1226 }
1227
1228 /**
1229  * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1230  */
1231 static int ll_lease_file_resync(struct obd_client_handle *och,
1232                                 struct inode *inode, unsigned long arg)
1233 {
1234         struct ll_sb_info *sbi = ll_i2sbi(inode);
1235         struct md_op_data *op_data;
1236         struct ll_ioc_lease_id ioc;
1237         __u64 data_version_unused;
1238         int rc;
1239         ENTRY;
1240
1241         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1242                                      LUSTRE_OPC_ANY, NULL);
1243         if (IS_ERR(op_data))
1244                 RETURN(PTR_ERR(op_data));
1245
1246         if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
1247                            sizeof(ioc)))
1248                 RETURN(-EFAULT);
1249
1250         /* before starting file resync, it's necessary to clean up page cache
1251          * in client memory, otherwise once the layout version is increased,
1252          * writing back cached data will be denied the OSTs. */
1253         rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1254         if (rc)
1255                 GOTO(out, rc);
1256
1257         op_data->op_lease_handle = och->och_lease_handle;
1258         op_data->op_mirror_id = ioc.lil_mirror_id;
1259         rc = md_file_resync(sbi->ll_md_exp, op_data);
1260         if (rc)
1261                 GOTO(out, rc);
1262
1263         EXIT;
1264 out:
1265         ll_finish_md_op_data(op_data);
1266         return rc;
1267 }
1268
1269 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1270 {
1271         struct ll_inode_info *lli = ll_i2info(inode);
1272         struct cl_object *obj = lli->lli_clob;
1273         struct cl_attr *attr = vvp_env_thread_attr(env);
1274         s64 atime;
1275         s64 mtime;
1276         s64 ctime;
1277         int rc = 0;
1278
1279         ENTRY;
1280
1281         ll_inode_size_lock(inode);
1282
1283         /* Merge timestamps the most recently obtained from MDS with
1284          * timestamps obtained from OSTs.
1285          *
1286          * Do not overwrite atime of inode because it may be refreshed
1287          * by file_accessed() function. If the read was served by cache
1288          * data, there is no RPC to be sent so that atime may not be
1289          * transferred to OSTs at all. MDT only updates atime at close time
1290          * if it's at least 'mdd.*.atime_diff' older.
1291          * All in all, the atime in Lustre does not strictly comply with
1292          * POSIX. Solving this problem needs to send an RPC to MDT for each
1293          * read, this will hurt performance.
1294          */
1295         if (ll_file_test_and_clear_flag(lli, LLIF_UPDATE_ATIME) ||
1296             inode->i_atime.tv_sec < lli->lli_atime)
1297                 inode->i_atime.tv_sec = lli->lli_atime;
1298
1299         inode->i_mtime.tv_sec = lli->lli_mtime;
1300         inode->i_ctime.tv_sec = lli->lli_ctime;
1301
1302         mtime = inode->i_mtime.tv_sec;
1303         atime = inode->i_atime.tv_sec;
1304         ctime = inode->i_ctime.tv_sec;
1305
1306         cl_object_attr_lock(obj);
1307         if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1308                 rc = -EINVAL;
1309         else
1310                 rc = cl_object_attr_get(env, obj, attr);
1311         cl_object_attr_unlock(obj);
1312
1313         if (rc != 0)
1314                 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1315
1316         if (atime < attr->cat_atime)
1317                 atime = attr->cat_atime;
1318
1319         if (ctime < attr->cat_ctime)
1320                 ctime = attr->cat_ctime;
1321
1322         if (mtime < attr->cat_mtime)
1323                 mtime = attr->cat_mtime;
1324
1325         CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1326                PFID(&lli->lli_fid), attr->cat_size);
1327
1328         i_size_write(inode, attr->cat_size);
1329         inode->i_blocks = attr->cat_blocks;
1330
1331         inode->i_mtime.tv_sec = mtime;
1332         inode->i_atime.tv_sec = atime;
1333         inode->i_ctime.tv_sec = ctime;
1334
1335 out_size_unlock:
1336         ll_inode_size_unlock(inode);
1337
1338         RETURN(rc);
1339 }
1340
1341 /**
1342  * Set designated mirror for I/O.
1343  *
1344  * So far only read, write, and truncated can support to issue I/O to
1345  * designated mirror.
1346  */
1347 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1348 {
1349         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1350
1351         /* clear layout version for generic(non-resync) I/O in case it carries
1352          * stale layout version due to I/O restart */
1353         io->ci_layout_version = 0;
1354
1355         /* FLR: disable non-delay for designated mirror I/O because obviously
1356          * only one mirror is available */
1357         if (fd->fd_designated_mirror > 0) {
1358                 io->ci_ndelay = 0;
1359                 io->ci_designated_mirror = fd->fd_designated_mirror;
1360                 io->ci_layout_version = fd->fd_layout_version;
1361         }
1362
1363         CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1364                file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1365 }
1366
1367 static bool file_is_noatime(const struct file *file)
1368 {
1369         const struct vfsmount *mnt = file->f_path.mnt;
1370         const struct inode *inode = file_inode((struct file *)file);
1371
1372         /* Adapted from file_accessed() and touch_atime().*/
1373         if (file->f_flags & O_NOATIME)
1374                 return true;
1375
1376         if (inode->i_flags & S_NOATIME)
1377                 return true;
1378
1379         if (IS_NOATIME(inode))
1380                 return true;
1381
1382         if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1383                 return true;
1384
1385         if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1386                 return true;
1387
1388         if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1389                 return true;
1390
1391         return false;
1392 }
1393
1394 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1395                 struct vvp_io_args *args)
1396 {
1397         struct inode *inode = file_inode(file);
1398         struct ll_file_data *fd  = LUSTRE_FPRIVATE(file);
1399
1400         io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1401         io->ci_lock_no_expand = fd->ll_lock_no_expand;
1402
1403         if (iot == CIT_WRITE) {
1404                 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1405                 io->u.ci_wr.wr_sync   = !!(file->f_flags & O_SYNC ||
1406                                            file->f_flags & O_DIRECT ||
1407                                            IS_SYNC(inode));
1408 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1409                 io->u.ci_wr.wr_sync  |= !!(args &&
1410                                            args->via_io_subtype == IO_NORMAL &&
1411                                            args->u.normal.via_iocb->ki_flags & IOCB_DSYNC);
1412 #endif
1413         }
1414
1415         io->ci_obj = ll_i2info(inode)->lli_clob;
1416         io->ci_lockreq = CILR_MAYBE;
1417         if (ll_file_nolock(file)) {
1418                 io->ci_lockreq = CILR_NEVER;
1419                 io->ci_no_srvlock = 1;
1420         } else if (file->f_flags & O_APPEND) {
1421                 io->ci_lockreq = CILR_MANDATORY;
1422         }
1423         io->ci_noatime = file_is_noatime(file);
1424         io->ci_async_readahead = false;
1425
1426         /* FLR: only use non-delay I/O for read as there is only one
1427          * avaliable mirror for write. */
1428         io->ci_ndelay = !(iot == CIT_WRITE);
1429
1430         ll_io_set_mirror(io, file);
1431 }
1432
1433 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1434                         __u64 count)
1435 {
1436         struct ll_inode_info *lli = ll_i2info(inode);
1437         struct ll_sb_info *sbi = ll_i2sbi(inode);
1438         enum obd_heat_type sample_type;
1439         enum obd_heat_type iobyte_type;
1440         __u64 now = ktime_get_real_seconds();
1441
1442         if (!ll_sbi_has_file_heat(sbi) ||
1443             lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1444                 return;
1445
1446         if (iot == CIT_READ) {
1447                 sample_type = OBD_HEAT_READSAMPLE;
1448                 iobyte_type = OBD_HEAT_READBYTE;
1449         } else if (iot == CIT_WRITE) {
1450                 sample_type = OBD_HEAT_WRITESAMPLE;
1451                 iobyte_type = OBD_HEAT_WRITEBYTE;
1452         } else {
1453                 return;
1454         }
1455
1456         spin_lock(&lli->lli_heat_lock);
1457         obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1458                      sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1459         obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1460                      sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1461         spin_unlock(&lli->lli_heat_lock);
1462 }
1463
1464 static ssize_t
1465 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1466                    struct file *file, enum cl_io_type iot,
1467                    loff_t *ppos, size_t count)
1468 {
1469         struct vvp_io           *vio = vvp_env_io(env);
1470         struct inode            *inode = file_inode(file);
1471         struct ll_inode_info    *lli = ll_i2info(inode);
1472         struct ll_file_data     *fd  = LUSTRE_FPRIVATE(file);
1473         struct range_lock       range;
1474         struct cl_io            *io;
1475         ssize_t                 result = 0;
1476         int                     rc = 0;
1477         unsigned                retried = 0;
1478         bool                    restarted = false;
1479
1480         ENTRY;
1481
1482         CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1483                 file_dentry(file)->d_name.name,
1484                 iot == CIT_READ ? "read" : "write", *ppos, count);
1485
1486 restart:
1487         io = vvp_env_thread_io(env);
1488         ll_io_init(io, file, iot, args);
1489         io->ci_ndelay_tried = retried;
1490
1491         if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
1492                 bool range_locked = false;
1493
1494                 if (file->f_flags & O_APPEND)
1495                         range_lock_init(&range, 0, LUSTRE_EOF);
1496                 else
1497                         range_lock_init(&range, *ppos, *ppos + count - 1);
1498
1499                 vio->vui_fd  = LUSTRE_FPRIVATE(file);
1500                 vio->vui_io_subtype = args->via_io_subtype;
1501
1502                 switch (vio->vui_io_subtype) {
1503                 case IO_NORMAL:
1504                         vio->vui_iter = args->u.normal.via_iter;
1505                         vio->vui_iocb = args->u.normal.via_iocb;
1506                         /* Direct IO reads must also take range lock,
1507                          * or multiple reads will try to work on the same pages
1508                          * See LU-6227 for details. */
1509                         if (((iot == CIT_WRITE) ||
1510                             (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1511                             !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1512                                 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1513                                        RL_PARA(&range));
1514                                 rc = range_lock(&lli->lli_write_tree, &range);
1515                                 if (rc < 0)
1516                                         GOTO(out, rc);
1517
1518                                 range_locked = true;
1519                         }
1520                         break;
1521                 case IO_SPLICE:
1522                         vio->u.splice.vui_pipe = args->u.splice.via_pipe;
1523                         vio->u.splice.vui_flags = args->u.splice.via_flags;
1524                         break;
1525                 default:
1526                         CERROR("unknown IO subtype %u\n", vio->vui_io_subtype);
1527                         LBUG();
1528                 }
1529
1530                 ll_cl_add(file, env, io, LCC_RW);
1531                 rc = cl_io_loop(env, io);
1532                 ll_cl_remove(file, env);
1533
1534                 if (range_locked) {
1535                         CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1536                                RL_PARA(&range));
1537                         range_unlock(&lli->lli_write_tree, &range);
1538                 }
1539         } else {
1540                 /* cl_io_rw_init() handled IO */
1541                 rc = io->ci_result;
1542         }
1543
1544         if (io->ci_nob > 0) {
1545                 result += io->ci_nob;
1546                 count  -= io->ci_nob;
1547                 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1548
1549                 /* prepare IO restart */
1550                 if (count > 0 && args->via_io_subtype == IO_NORMAL)
1551                         args->u.normal.via_iter = vio->vui_iter;
1552         }
1553 out:
1554         cl_io_fini(env, io);
1555
1556         CDEBUG(D_VFSTRACE,
1557                "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1558                file->f_path.dentry->d_name.name,
1559                iot, rc, result, io->ci_need_restart);
1560
1561         if ((rc == 0 || rc == -ENODATA) && count > 0 && io->ci_need_restart) {
1562                 CDEBUG(D_VFSTRACE,
1563                        "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1564                        file_dentry(file)->d_name.name,
1565                        iot == CIT_READ ? "read" : "write",
1566                        *ppos, count, result, rc);
1567                 /* preserve the tried count for FLR */
1568                 retried = io->ci_ndelay_tried;
1569                 restarted = true;
1570                 goto restart;
1571         }
1572
1573         if (iot == CIT_READ) {
1574                 if (result > 0)
1575                         ll_stats_ops_tally(ll_i2sbi(inode),
1576                                            LPROC_LL_READ_BYTES, result);
1577         } else if (iot == CIT_WRITE) {
1578                 if (result > 0) {
1579                         ll_stats_ops_tally(ll_i2sbi(inode),
1580                                            LPROC_LL_WRITE_BYTES, result);
1581                         fd->fd_write_failed = false;
1582                 } else if (result == 0 && rc == 0) {
1583                         rc = io->ci_result;
1584                         if (rc < 0)
1585                                 fd->fd_write_failed = true;
1586                         else
1587                                 fd->fd_write_failed = false;
1588                 } else if (rc != -ERESTARTSYS) {
1589                         fd->fd_write_failed = true;
1590                 }
1591         }
1592
1593         CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1594         if (result > 0)
1595                 ll_heat_add(inode, iot, result);
1596
1597         RETURN(result > 0 ? result : rc);
1598 }
1599
1600 /**
1601  * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1602  * especially for small I/O.
1603  *
1604  * To serve a read request, CLIO has to create and initialize a cl_io and
1605  * then request DLM lock. This has turned out to have siginificant overhead
1606  * and affects the performance of small I/O dramatically.
1607  *
1608  * It's not necessary to create a cl_io for each I/O. Under the help of read
1609  * ahead, most of the pages being read are already in memory cache and we can
1610  * read those pages directly because if the pages exist, the corresponding DLM
1611  * lock must exist so that page content must be valid.
1612  *
1613  * In fast read implementation, the llite speculatively finds and reads pages
1614  * in memory cache. There are three scenarios for fast read:
1615  *   - If the page exists and is uptodate, kernel VM will provide the data and
1616  *     CLIO won't be intervened;
1617  *   - If the page was brought into memory by read ahead, it will be exported
1618  *     and read ahead parameters will be updated;
1619  *   - Otherwise the page is not in memory, we can't do fast read. Therefore,
1620  *     it will go back and invoke normal read, i.e., a cl_io will be created
1621  *     and DLM lock will be requested.
1622  *
1623  * POSIX compliance: posix standard states that read is intended to be atomic.
1624  * Lustre read implementation is in line with Linux kernel read implementation
1625  * and neither of them complies with POSIX standard in this matter. Fast read
1626  * doesn't make the situation worse on single node but it may interleave write
1627  * results from multiple nodes due to short read handling in ll_file_aio_read().
1628  *
1629  * \param env - lu_env
1630  * \param iocb - kiocb from kernel
1631  * \param iter - user space buffers where the data will be copied
1632  *
1633  * \retval - number of bytes have been read, or error code if error occurred.
1634  */
1635 static ssize_t
1636 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1637 {
1638         ssize_t result;
1639
1640         if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1641                 return 0;
1642
1643         /* NB: we can't do direct IO for fast read because it will need a lock
1644          * to make IO engine happy. */
1645         if (iocb->ki_filp->f_flags & O_DIRECT)
1646                 return 0;
1647
1648         result = generic_file_read_iter(iocb, iter);
1649
1650         /* If the first page is not in cache, generic_file_aio_read() will be
1651          * returned with -ENODATA.
1652          * See corresponding code in ll_readpage(). */
1653         if (result == -ENODATA)
1654                 result = 0;
1655
1656         if (result > 0) {
1657                 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1658                 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1659                                    LPROC_LL_READ_BYTES, result);
1660         }
1661
1662         return result;
1663 }
1664
1665 /*
1666  * Read from a file (through the page cache).
1667  */
1668 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1669 {
1670         struct lu_env *env;
1671         struct vvp_io_args *args;
1672         struct file *file = iocb->ki_filp;
1673         ssize_t result;
1674         ssize_t rc2;
1675         __u16 refcheck;
1676         ktime_t kstart = ktime_get();
1677         bool cached;
1678
1679         if (!iov_iter_count(to))
1680                 return 0;
1681
1682         /**
1683          * Currently when PCC read failed, we do not fall back to the
1684          * normal read path, just return the error.
1685          * The resaon is that: for RW-PCC, the file data may be modified
1686          * in the PCC and inconsistent with the data on OSTs (or file
1687          * data has been removed from the Lustre file system), at this
1688          * time, fallback to the normal read path may read the wrong
1689          * data.
1690          * TODO: for RO-PCC (readonly PCC), fall back to normal read
1691          * path: read data from data copy on OSTs.
1692          */
1693         result = pcc_file_read_iter(iocb, to, &cached);
1694         if (cached)
1695                 GOTO(out, result);
1696
1697         ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
1698
1699         result = ll_do_fast_read(iocb, to);
1700         if (result < 0 || iov_iter_count(to) == 0)
1701                 GOTO(out, result);
1702
1703         env = cl_env_get(&refcheck);
1704         if (IS_ERR(env))
1705                 return PTR_ERR(env);
1706
1707         args = ll_env_args(env, IO_NORMAL);
1708         args->u.normal.via_iter = to;
1709         args->u.normal.via_iocb = iocb;
1710
1711         rc2 = ll_file_io_generic(env, args, file, CIT_READ,
1712                                  &iocb->ki_pos, iov_iter_count(to));
1713         if (rc2 > 0)
1714                 result += rc2;
1715         else if (result == 0)
1716                 result = rc2;
1717
1718         cl_env_put(env, &refcheck);
1719 out:
1720         if (result > 0) {
1721                 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1722                                   LUSTRE_FPRIVATE(file), iocb->ki_pos, result,
1723                                   READ);
1724                 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
1725                                    ktime_us_delta(ktime_get(), kstart));
1726         }
1727
1728         return result;
1729 }
1730
1731 /**
1732  * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1733  * If a page is already in the page cache and dirty (and some other things -
1734  * See ll_tiny_write_begin for the instantiation of these rules), then we can
1735  * write to it without doing a full I/O, because Lustre already knows about it
1736  * and will write it out.  This saves a lot of processing time.
1737  *
1738  * All writes here are within one page, so exclusion is handled by the page
1739  * lock on the vm page.  We do not do tiny writes for writes which touch
1740  * multiple pages because it's very unlikely multiple sequential pages are
1741  * are already dirty.
1742  *
1743  * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1744  * and are unlikely to be to already dirty pages.
1745  *
1746  * Attribute updates are important here, we do them in ll_tiny_write_end.
1747  */
1748 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1749 {
1750         ssize_t count = iov_iter_count(iter);
1751         struct  file *file = iocb->ki_filp;
1752         struct  inode *inode = file_inode(file);
1753         bool    lock_inode = !IS_NOSEC(inode);
1754         ssize_t result = 0;
1755
1756         ENTRY;
1757
1758         /* Restrict writes to single page and < PAGE_SIZE.  See comment at top
1759          * of function for why.
1760          */
1761         if (count >= PAGE_SIZE ||
1762             (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
1763                 RETURN(0);
1764
1765         if (unlikely(lock_inode))
1766                 inode_lock(inode);
1767         result = __generic_file_write_iter(iocb, iter);
1768
1769         if (unlikely(lock_inode))
1770                 inode_unlock(inode);
1771
1772         /* If the page is not already dirty, ll_tiny_write_begin returns
1773          * -ENODATA.  We continue on to normal write.
1774          */
1775         if (result == -ENODATA)
1776                 result = 0;
1777
1778         if (result > 0) {
1779                 ll_heat_add(inode, CIT_WRITE, result);
1780                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
1781                                    result);
1782                 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1783         }
1784
1785         CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
1786
1787         RETURN(result);
1788 }
1789
1790 /*
1791  * Write to a file (through the page cache).
1792  */
1793 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1794 {
1795         struct vvp_io_args *args;
1796         struct lu_env *env;
1797         ssize_t rc_tiny = 0, rc_normal;
1798         struct file *file = iocb->ki_filp;
1799         __u16 refcheck;
1800         bool cached;
1801         ktime_t kstart = ktime_get();
1802         int result;
1803
1804         ENTRY;
1805
1806         if (!iov_iter_count(from))
1807                 GOTO(out, rc_normal = 0);
1808
1809         /**
1810          * When PCC write failed, we usually do not fall back to the normal
1811          * write path, just return the error. But there is a special case when
1812          * returned error code is -ENOSPC due to running out of space on PCC HSM
1813          * bakcend. At this time, it will fall back to normal I/O path and
1814          * retry the I/O. As the file is in HSM released state, it will restore
1815          * the file data to OSTs first and redo the write again. And the
1816          * restore process will revoke the layout lock and detach the file
1817          * from PCC cache automatically.
1818          */
1819         result = pcc_file_write_iter(iocb, from, &cached);
1820         if (cached && result != -ENOSPC && result != -EDQUOT)
1821                 GOTO(out, rc_normal = result);
1822
1823         /* NB: we can't do direct IO for tiny writes because they use the page
1824          * cache, we can't do sync writes because tiny writes can't flush
1825          * pages, and we can't do append writes because we can't guarantee the
1826          * required DLM locks are held to protect file size.
1827          */
1828         if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
1829             !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
1830                 rc_tiny = ll_do_tiny_write(iocb, from);
1831
1832         /* In case of error, go on and try normal write - Only stop if tiny
1833          * write completed I/O.
1834          */
1835         if (iov_iter_count(from) == 0)
1836                 GOTO(out, rc_normal = rc_tiny);
1837
1838         env = cl_env_get(&refcheck);
1839         if (IS_ERR(env))
1840                 return PTR_ERR(env);
1841
1842         args = ll_env_args(env, IO_NORMAL);
1843         args->u.normal.via_iter = from;
1844         args->u.normal.via_iocb = iocb;
1845
1846         rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
1847                                        &iocb->ki_pos, iov_iter_count(from));
1848
1849         /* On success, combine bytes written. */
1850         if (rc_tiny >= 0 && rc_normal > 0)
1851                 rc_normal += rc_tiny;
1852         /* On error, only return error from normal write if tiny write did not
1853          * write any bytes.  Otherwise return bytes written by tiny write.
1854          */
1855         else if (rc_tiny > 0)
1856                 rc_normal = rc_tiny;
1857
1858         cl_env_put(env, &refcheck);
1859 out:
1860         if (rc_normal > 0) {
1861                 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1862                                   LUSTRE_FPRIVATE(file), iocb->ki_pos,
1863                                   rc_normal, WRITE);
1864                 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
1865                                    ktime_us_delta(ktime_get(), kstart));
1866         }
1867
1868         RETURN(rc_normal);
1869 }
1870
1871 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1872 /*
1873  * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
1874  */
1875 static int ll_file_get_iov_count(const struct iovec *iov,
1876                                  unsigned long *nr_segs, size_t *count)
1877 {
1878         size_t cnt = 0;
1879         unsigned long seg;
1880
1881         for (seg = 0; seg < *nr_segs; seg++) {
1882                 const struct iovec *iv = &iov[seg];
1883
1884                 /*
1885                  * If any segment has a negative length, or the cumulative
1886                  * length ever wraps negative then return -EINVAL.
1887                  */
1888                 cnt += iv->iov_len;
1889                 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1890                         return -EINVAL;
1891                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
1892                         continue;
1893                 if (seg == 0)
1894                         return -EFAULT;
1895                 *nr_segs = seg;
1896                 cnt -= iv->iov_len;     /* This segment is no good */
1897                 break;
1898         }
1899         *count = cnt;
1900         return 0;
1901 }
1902
1903 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1904                                 unsigned long nr_segs, loff_t pos)
1905 {
1906         struct iov_iter to;
1907         size_t iov_count;
1908         ssize_t result;
1909         ENTRY;
1910
1911         result = ll_file_get_iov_count(iov, &nr_segs, &iov_count);
1912         if (result)
1913                 RETURN(result);
1914
1915         if (!iov_count)
1916                 RETURN(0);
1917
1918 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1919         iov_iter_init(&to, READ, iov, nr_segs, iov_count);
1920 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1921         iov_iter_init(&to, iov, nr_segs, iov_count, 0);
1922 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1923
1924         result = ll_file_read_iter(iocb, &to);
1925
1926         RETURN(result);
1927 }
1928
1929 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
1930                             loff_t *ppos)
1931 {
1932         struct iovec   iov = { .iov_base = buf, .iov_len = count };
1933         struct kiocb   kiocb;
1934         ssize_t        result;
1935
1936         ENTRY;
1937
1938         if (!count)
1939                 RETURN(0);
1940
1941         init_sync_kiocb(&kiocb, file);
1942         kiocb.ki_pos = *ppos;
1943 #ifdef HAVE_KIOCB_KI_LEFT
1944         kiocb.ki_left = count;
1945 #elif defined(HAVE_KI_NBYTES)
1946         kiocb.i_nbytes = count;
1947 #endif
1948
1949         result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
1950         *ppos = kiocb.ki_pos;
1951
1952         RETURN(result);
1953 }
1954
1955 /*
1956  * Write to a file (through the page cache).
1957  * AIO stuff
1958  */
1959 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1960                                  unsigned long nr_segs, loff_t pos)
1961 {
1962         struct iov_iter from;
1963         size_t iov_count;
1964         ssize_t result;
1965         ENTRY;
1966
1967         result = ll_file_get_iov_count(iov, &nr_segs, &iov_count);
1968         if (result)
1969                 RETURN(result);
1970
1971         if (!iov_count)
1972                 RETURN(0);
1973
1974 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1975         iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
1976 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1977         iov_iter_init(&from, iov, nr_segs, iov_count, 0);
1978 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1979
1980         result = ll_file_write_iter(iocb, &from);
1981
1982         RETURN(result);
1983 }
1984
1985 static ssize_t ll_file_write(struct file *file, const char __user *buf,
1986                              size_t count, loff_t *ppos)
1987 {
1988         struct iovec   iov = { .iov_base = (void __user *)buf,
1989                                .iov_len = count };
1990         struct kiocb   kiocb;
1991         ssize_t        result;
1992
1993         ENTRY;
1994
1995         if (!count)
1996                 RETURN(0);
1997
1998         init_sync_kiocb(&kiocb, file);
1999         kiocb.ki_pos = *ppos;
2000 #ifdef HAVE_KIOCB_KI_LEFT
2001         kiocb.ki_left = count;
2002 #elif defined(HAVE_KI_NBYTES)
2003         kiocb.ki_nbytes = count;
2004 #endif
2005
2006         result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2007         *ppos = kiocb.ki_pos;
2008
2009         RETURN(result);
2010 }
2011 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2012
2013 /*
2014  * Send file content (through pagecache) somewhere with helper
2015  */
2016 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
2017                                    struct pipe_inode_info *pipe, size_t count,
2018                                    unsigned int flags)
2019 {
2020         struct lu_env *env;
2021         struct vvp_io_args *args;
2022         ssize_t result;
2023         __u16 refcheck;
2024         bool cached;
2025
2026         ENTRY;
2027
2028         result = pcc_file_splice_read(in_file, ppos, pipe,
2029                                       count, flags, &cached);
2030         if (cached)
2031                 RETURN(result);
2032
2033         ll_ras_enter(in_file, *ppos, count);
2034
2035         env = cl_env_get(&refcheck);
2036         if (IS_ERR(env))
2037                 RETURN(PTR_ERR(env));
2038
2039         args = ll_env_args(env, IO_SPLICE);
2040         args->u.splice.via_pipe = pipe;
2041         args->u.splice.via_flags = flags;
2042
2043         result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
2044         cl_env_put(env, &refcheck);
2045
2046         if (result > 0)
2047                 ll_rw_stats_tally(ll_i2sbi(file_inode(in_file)), current->pid,
2048                                   LUSTRE_FPRIVATE(in_file), *ppos, result,
2049                                   READ);
2050         RETURN(result);
2051 }
2052
2053 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2054                              __u64 flags, struct lov_user_md *lum, int lum_size)
2055 {
2056         struct lookup_intent oit = {
2057                 .it_op = IT_OPEN,
2058                 .it_flags = flags | MDS_OPEN_BY_FID,
2059         };
2060         int rc;
2061         ENTRY;
2062
2063         if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2064             le32_to_cpu(LOV_MAGIC_MAGIC)) {
2065                 /* this code will only exist for big-endian systems */
2066                 lustre_swab_lov_user_md(lum, 0);
2067         }
2068
2069         ll_inode_size_lock(inode);
2070         rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2071         if (rc < 0)
2072                 GOTO(out_unlock, rc);
2073
2074         ll_release_openhandle(dentry, &oit);
2075
2076 out_unlock:
2077         ll_inode_size_unlock(inode);
2078         ll_intent_release(&oit);
2079
2080         RETURN(rc);
2081 }
2082
2083 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2084                              struct lov_mds_md **lmmp, int *lmm_size,
2085                              struct ptlrpc_request **request)
2086 {
2087         struct ll_sb_info *sbi = ll_i2sbi(inode);
2088         struct mdt_body  *body;
2089         struct lov_mds_md *lmm = NULL;
2090         struct ptlrpc_request *req = NULL;
2091         struct md_op_data *op_data;
2092         int rc, lmmsize;
2093
2094         rc = ll_get_default_mdsize(sbi, &lmmsize);
2095         if (rc)
2096                 RETURN(rc);
2097
2098         op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2099                                      strlen(filename), lmmsize,
2100                                      LUSTRE_OPC_ANY, NULL);
2101         if (IS_ERR(op_data))
2102                 RETURN(PTR_ERR(op_data));
2103
2104         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2105         rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2106         ll_finish_md_op_data(op_data);
2107         if (rc < 0) {
2108                 CDEBUG(D_INFO, "md_getattr_name failed "
2109                        "on %s: rc %d\n", filename, rc);
2110                 GOTO(out, rc);
2111         }
2112
2113         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2114         LASSERT(body != NULL); /* checked by mdc_getattr_name */
2115
2116         lmmsize = body->mbo_eadatasize;
2117
2118         if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2119                         lmmsize == 0) {
2120                 GOTO(out, rc = -ENODATA);
2121         }
2122
2123         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2124         LASSERT(lmm != NULL);
2125
2126         if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2127             lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2128             lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2129             lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2130                 GOTO(out, rc = -EPROTO);
2131
2132         /*
2133          * This is coming from the MDS, so is probably in
2134          * little endian.  We convert it to host endian before
2135          * passing it to userspace.
2136          */
2137         if ((lmm->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
2138             __swab32(LOV_MAGIC_MAGIC)) {
2139                 int stripe_count = 0;
2140
2141                 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2142                     lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2143                         stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2144                         if (le32_to_cpu(lmm->lmm_pattern) &
2145                             LOV_PATTERN_F_RELEASED)
2146                                 stripe_count = 0;
2147                 }
2148
2149                 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2150
2151                 /* if function called for directory - we should
2152                  * avoid swab not existent lsm objects */
2153                 if (lmm->lmm_magic == LOV_MAGIC_V1 && S_ISREG(body->mbo_mode))
2154                         lustre_swab_lov_user_md_objects(
2155                                 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2156                                 stripe_count);
2157                 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2158                          S_ISREG(body->mbo_mode))
2159                         lustre_swab_lov_user_md_objects(
2160                                 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2161                                 stripe_count);
2162         }
2163
2164 out:
2165         *lmmp = lmm;
2166         *lmm_size = lmmsize;
2167         *request = req;
2168         return rc;
2169 }
2170
2171 static int ll_lov_setea(struct inode *inode, struct file *file,
2172                         void __user *arg)
2173 {
2174         __u64                    flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2175         struct lov_user_md      *lump;
2176         int                      lum_size = sizeof(struct lov_user_md) +
2177                                             sizeof(struct lov_user_ost_data);
2178         int                      rc;
2179         ENTRY;
2180
2181         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2182                 RETURN(-EPERM);
2183
2184         OBD_ALLOC_LARGE(lump, lum_size);
2185         if (lump == NULL)
2186                 RETURN(-ENOMEM);
2187
2188         if (copy_from_user(lump, arg, lum_size))
2189                 GOTO(out_lump, rc = -EFAULT);
2190
2191         rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2192                                       lum_size);
2193         cl_lov_delay_create_clear(&file->f_flags);
2194
2195 out_lump:
2196         OBD_FREE_LARGE(lump, lum_size);
2197         RETURN(rc);
2198 }
2199
2200 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2201 {
2202         struct lu_env   *env;
2203         __u16           refcheck;
2204         int             rc;
2205         ENTRY;
2206
2207         env = cl_env_get(&refcheck);
2208         if (IS_ERR(env))
2209                 RETURN(PTR_ERR(env));
2210
2211         rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2212         cl_env_put(env, &refcheck);
2213         RETURN(rc);
2214 }
2215
2216 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2217                             void __user *arg)
2218 {
2219         struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2220         struct lov_user_md        *klum;
2221         int                        lum_size, rc;
2222         __u64                      flags = FMODE_WRITE;
2223         ENTRY;
2224
2225         rc = ll_copy_user_md(lum, &klum);
2226         if (rc < 0)
2227                 RETURN(rc);
2228
2229         lum_size = rc;
2230         rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2231                                       lum_size);
2232         if (!rc) {
2233                 __u32 gen;
2234
2235                 rc = put_user(0, &lum->lmm_stripe_count);
2236                 if (rc)
2237                         GOTO(out, rc);
2238
2239                 rc = ll_layout_refresh(inode, &gen);
2240                 if (rc)
2241                         GOTO(out, rc);
2242
2243                 rc = ll_file_getstripe(inode, arg, lum_size);
2244         }
2245         cl_lov_delay_create_clear(&file->f_flags);
2246
2247 out:
2248         OBD_FREE_LARGE(klum, lum_size);
2249         RETURN(rc);
2250 }
2251
2252
2253 static int
2254 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2255 {
2256         struct ll_inode_info *lli = ll_i2info(inode);
2257         struct cl_object *obj = lli->lli_clob;
2258         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2259         struct ll_grouplock grouplock;
2260         int rc;
2261         ENTRY;
2262
2263         if (arg == 0) {
2264                 CWARN("group id for group lock must not be 0\n");
2265                 RETURN(-EINVAL);
2266         }
2267
2268         if (ll_file_nolock(file))
2269                 RETURN(-EOPNOTSUPP);
2270 retry:
2271         if (file->f_flags & O_NONBLOCK) {
2272                 if (!mutex_trylock(&lli->lli_group_mutex))
2273                         RETURN(-EAGAIN);
2274         } else
2275                 mutex_lock(&lli->lli_group_mutex);
2276
2277         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2278                 CWARN("group lock already existed with gid %lu\n",
2279                       fd->fd_grouplock.lg_gid);
2280                 GOTO(out, rc = -EINVAL);
2281         }
2282         if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2283                 if (file->f_flags & O_NONBLOCK)
2284                         GOTO(out, rc = -EAGAIN);
2285                 mutex_unlock(&lli->lli_group_mutex);
2286                 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2287                 GOTO(retry, rc = 0);
2288         }
2289         LASSERT(fd->fd_grouplock.lg_lock == NULL);
2290
2291         /**
2292          * XXX: group lock needs to protect all OST objects while PFL
2293          * can add new OST objects during the IO, so we'd instantiate
2294          * all OST objects before getting its group lock.
2295          */
2296         if (obj) {
2297                 struct lu_env *env;
2298                 __u16 refcheck;
2299                 struct cl_layout cl = {
2300                         .cl_is_composite = false,
2301                 };
2302                 struct lu_extent ext = {
2303                         .e_start = 0,
2304                         .e_end = OBD_OBJECT_EOF,
2305                 };
2306
2307                 env = cl_env_get(&refcheck);
2308                 if (IS_ERR(env))
2309                         GOTO(out, rc = PTR_ERR(env));
2310
2311                 rc = cl_object_layout_get(env, obj, &cl);
2312                 if (!rc && cl.cl_is_composite)
2313                         rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2314                                                     &ext);
2315
2316                 cl_env_put(env, &refcheck);
2317                 if (rc)
2318                         GOTO(out, rc);
2319         }
2320
2321         rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2322                               arg, (file->f_flags & O_NONBLOCK), &grouplock);
2323
2324         if (rc)
2325                 GOTO(out, rc);
2326
2327         fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2328         fd->fd_grouplock = grouplock;
2329         if (lli->lli_group_users == 0)
2330                 lli->lli_group_gid = grouplock.lg_gid;
2331         lli->lli_group_users++;
2332
2333         CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2334 out:
2335         mutex_unlock(&lli->lli_group_mutex);
2336
2337         RETURN(rc);
2338 }
2339
2340 static int ll_put_grouplock(struct inode *inode, struct file *file,
2341                             unsigned long arg)
2342 {
2343         struct ll_inode_info   *lli = ll_i2info(inode);
2344         struct ll_file_data    *fd = LUSTRE_FPRIVATE(file);
2345         struct ll_grouplock     grouplock;
2346         int                     rc;
2347         ENTRY;
2348
2349         mutex_lock(&lli->lli_group_mutex);
2350         if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2351                 CWARN("no group lock held\n");
2352                 GOTO(out, rc = -EINVAL);
2353         }
2354
2355         LASSERT(fd->fd_grouplock.lg_lock != NULL);
2356
2357         if (fd->fd_grouplock.lg_gid != arg) {
2358                 CWARN("group lock %lu doesn't match current id %lu\n",
2359                       arg, fd->fd_grouplock.lg_gid);
2360                 GOTO(out, rc = -EINVAL);
2361         }
2362
2363         grouplock = fd->fd_grouplock;
2364         memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2365         fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2366
2367         cl_put_grouplock(&grouplock);
2368
2369         lli->lli_group_users--;
2370         if (lli->lli_group_users == 0) {
2371                 lli->lli_group_gid = 0;
2372                 wake_up_var(&lli->lli_group_users);
2373         }
2374         CDEBUG(D_INFO, "group lock %lu released\n", arg);
2375         GOTO(out, rc = 0);
2376 out:
2377         mutex_unlock(&lli->lli_group_mutex);
2378
2379         RETURN(rc);
2380 }
2381
2382 /**
2383  * Close inode open handle
2384  *
2385  * \param dentry [in]     dentry which contains the inode
2386  * \param it     [in,out] intent which contains open info and result
2387  *
2388  * \retval 0     success
2389  * \retval <0    failure
2390  */
2391 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2392 {
2393         struct inode *inode = dentry->d_inode;
2394         struct obd_client_handle *och;
2395         int rc;
2396         ENTRY;
2397
2398         LASSERT(inode);
2399
2400         /* Root ? Do nothing. */
2401         if (dentry->d_inode->i_sb->s_root == dentry)
2402                 RETURN(0);
2403
2404         /* No open handle to close? Move away */
2405         if (!it_disposition(it, DISP_OPEN_OPEN))
2406                 RETURN(0);
2407
2408         LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2409
2410         OBD_ALLOC(och, sizeof(*och));
2411         if (!och)
2412                 GOTO(out, rc = -ENOMEM);
2413
2414         rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2415         if (rc)
2416                 GOTO(out, rc);
2417
2418         rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2419 out:
2420         /* this one is in place of ll_file_open */
2421         if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2422                 ptlrpc_req_finished(it->it_request);
2423                 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2424         }
2425         RETURN(rc);
2426 }
2427
2428 /**
2429  * Get size for inode for which FIEMAP mapping is requested.
2430  * Make the FIEMAP get_info call and returns the result.
2431  * \param fiemap        kernel buffer to hold extens
2432  * \param num_bytes     kernel buffer size
2433  */
2434 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2435                         size_t num_bytes)
2436 {
2437         struct lu_env                   *env;
2438         __u16                           refcheck;
2439         int                             rc = 0;
2440         struct ll_fiemap_info_key       fmkey = { .lfik_name = KEY_FIEMAP, };
2441         ENTRY;
2442
2443         /* Checks for fiemap flags */
2444         if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2445                 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2446                 return -EBADR;
2447         }
2448
2449         /* Check for FIEMAP_FLAG_SYNC */
2450         if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2451                 rc = filemap_fdatawrite(inode->i_mapping);
2452                 if (rc)
2453                         return rc;
2454         }
2455
2456         env = cl_env_get(&refcheck);
2457         if (IS_ERR(env))
2458                 RETURN(PTR_ERR(env));
2459
2460         if (i_size_read(inode) == 0) {
2461                 rc = ll_glimpse_size(inode);
2462                 if (rc)
2463                         GOTO(out, rc);
2464         }
2465
2466         fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2467         obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2468         obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2469
2470         /* If filesize is 0, then there would be no objects for mapping */
2471         if (fmkey.lfik_oa.o_size == 0) {
2472                 fiemap->fm_mapped_extents = 0;
2473                 GOTO(out, rc = 0);
2474         }
2475
2476         fmkey.lfik_fiemap = *fiemap;
2477
2478         rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2479                               &fmkey, fiemap, &num_bytes);
2480 out:
2481         cl_env_put(env, &refcheck);
2482         RETURN(rc);
2483 }
2484
2485 int ll_fid2path(struct inode *inode, void __user *arg)
2486 {
2487         struct obd_export       *exp = ll_i2mdexp(inode);
2488         const struct getinfo_fid2path __user *gfin = arg;
2489         __u32                    pathlen;
2490         struct getinfo_fid2path *gfout;
2491         size_t                   outsize;
2492         int                      rc;
2493
2494         ENTRY;
2495
2496         if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
2497             !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2498                 RETURN(-EPERM);
2499
2500         /* Only need to get the buflen */
2501         if (get_user(pathlen, &gfin->gf_pathlen))
2502                 RETURN(-EFAULT);
2503
2504         if (pathlen > PATH_MAX)
2505                 RETURN(-EINVAL);
2506
2507         outsize = sizeof(*gfout) + pathlen;
2508         OBD_ALLOC(gfout, outsize);
2509         if (gfout == NULL)
2510                 RETURN(-ENOMEM);
2511
2512         if (copy_from_user(gfout, arg, sizeof(*gfout)))
2513                 GOTO(gf_free, rc = -EFAULT);
2514         /* append root FID after gfout to let MDT know the root FID so that it
2515          * can lookup the correct path, this is mainly for fileset.
2516          * old server without fileset mount support will ignore this. */
2517         *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2518
2519         /* Call mdc_iocontrol */
2520         rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2521         if (rc != 0)
2522                 GOTO(gf_free, rc);
2523
2524         if (copy_to_user(arg, gfout, outsize))
2525                 rc = -EFAULT;
2526
2527 gf_free:
2528         OBD_FREE(gfout, outsize);
2529         RETURN(rc);
2530 }
2531
2532 static int
2533 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2534 {
2535         struct cl_object *obj = ll_i2info(inode)->lli_clob;
2536         struct lu_env *env;
2537         struct cl_io *io;
2538         __u16  refcheck;
2539         int result;
2540
2541         ENTRY;
2542
2543         ioc->idv_version = 0;
2544         ioc->idv_layout_version = UINT_MAX;
2545
2546         /* If no file object initialized, we consider its version is 0. */
2547         if (obj == NULL)
2548                 RETURN(0);
2549
2550         env = cl_env_get(&refcheck);
2551         if (IS_ERR(env))
2552                 RETURN(PTR_ERR(env));
2553
2554         io = vvp_env_thread_io(env);
2555         io->ci_obj = obj;
2556         io->u.ci_data_version.dv_data_version = 0;
2557         io->u.ci_data_version.dv_layout_version = UINT_MAX;
2558         io->u.ci_data_version.dv_flags = ioc->idv_flags;
2559
2560 restart:
2561         if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2562                 result = cl_io_loop(env, io);
2563         else
2564                 result = io->ci_result;
2565
2566         ioc->idv_version = io->u.ci_data_version.dv_data_version;
2567         ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2568
2569         cl_io_fini(env, io);
2570
2571         if (unlikely(io->ci_need_restart))
2572                 goto restart;
2573
2574         cl_env_put(env, &refcheck);
2575
2576         RETURN(result);
2577 }
2578
2579 /*
2580  * Read the data_version for inode.
2581  *
2582  * This value is computed using stripe object version on OST.
2583  * Version is computed using server side locking.
2584  *
2585  * @param flags if do sync on the OST side;
2586  *              0: no sync
2587  *              LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2588  *              LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2589  */
2590 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2591 {
2592         struct ioc_data_version ioc = { .idv_flags = flags };
2593         int rc;
2594
2595         rc = ll_ioc_data_version(inode, &ioc);
2596         if (!rc)
2597                 *data_version = ioc.idv_version;
2598
2599         return rc;
2600 }
2601
2602 /*
2603  * Trigger a HSM release request for the provided inode.
2604  */
2605 int ll_hsm_release(struct inode *inode)
2606 {
2607         struct lu_env *env;
2608         struct obd_client_handle *och = NULL;
2609         __u64 data_version = 0;
2610         int rc;
2611         __u16 refcheck;
2612         ENTRY;
2613
2614         CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2615                ll_i2sbi(inode)->ll_fsname,
2616                PFID(&ll_i2info(inode)->lli_fid));
2617
2618         och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2619         if (IS_ERR(och))
2620                 GOTO(out, rc = PTR_ERR(och));
2621
2622         /* Grab latest data_version and [am]time values */
2623         rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2624         if (rc != 0)
2625                 GOTO(out, rc);
2626
2627         env = cl_env_get(&refcheck);
2628         if (IS_ERR(env))
2629                 GOTO(out, rc = PTR_ERR(env));
2630
2631         rc = ll_merge_attr(env, inode);
2632         cl_env_put(env, &refcheck);
2633
2634         /* If error happen, we have the wrong size for a file.
2635          * Don't release it.
2636          */
2637         if (rc != 0)
2638                 GOTO(out, rc);
2639
2640         /* Release the file.
2641          * NB: lease lock handle is released in mdc_hsm_release_pack() because
2642          * we still need it to pack l_remote_handle to MDT. */
2643         rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2644                                        &data_version);
2645         och = NULL;
2646
2647         EXIT;
2648 out:
2649         if (och != NULL && !IS_ERR(och)) /* close the file */
2650                 ll_lease_close(och, inode, NULL);
2651
2652         return rc;
2653 }
2654
2655 struct ll_swap_stack {
2656         __u64                    dv1;
2657         __u64                    dv2;
2658         struct inode            *inode1;
2659         struct inode            *inode2;
2660         bool                     check_dv1;
2661         bool                     check_dv2;
2662 };
2663
2664 static int ll_swap_layouts(struct file *file1, struct file *file2,
2665                            struct lustre_swap_layouts *lsl)
2666 {
2667         struct mdc_swap_layouts  msl;
2668         struct md_op_data       *op_data;
2669         __u32                    gid;
2670         __u64                    dv;
2671         struct ll_swap_stack    *llss = NULL;
2672         int                      rc;
2673
2674         OBD_ALLOC_PTR(llss);
2675         if (llss == NULL)
2676                 RETURN(-ENOMEM);
2677
2678         llss->inode1 = file_inode(file1);
2679         llss->inode2 = file_inode(file2);
2680
2681         rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2682         if (rc < 0)
2683                 GOTO(free, rc);
2684
2685         /* we use 2 bool because it is easier to swap than 2 bits */
2686         if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2687                 llss->check_dv1 = true;
2688
2689         if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2690                 llss->check_dv2 = true;
2691
2692         /* we cannot use lsl->sl_dvX directly because we may swap them */
2693         llss->dv1 = lsl->sl_dv1;
2694         llss->dv2 = lsl->sl_dv2;
2695
2696         rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2697         if (rc == 0) /* same file, done! */
2698                 GOTO(free, rc);
2699
2700         if (rc < 0) { /* sequentialize it */
2701                 swap(llss->inode1, llss->inode2);
2702                 swap(file1, file2);
2703                 swap(llss->dv1, llss->dv2);
2704                 swap(llss->check_dv1, llss->check_dv2);
2705         }
2706
2707         gid = lsl->sl_gid;
2708         if (gid != 0) { /* application asks to flush dirty cache */
2709                 rc = ll_get_grouplock(llss->inode1, file1, gid);
2710                 if (rc < 0)
2711                         GOTO(free, rc);
2712
2713                 rc = ll_get_grouplock(llss->inode2, file2, gid);
2714                 if (rc < 0) {
2715                         ll_put_grouplock(llss->inode1, file1, gid);
2716                         GOTO(free, rc);
2717                 }
2718         }
2719
2720         /* ultimate check, before swaping the layouts we check if
2721          * dataversion has changed (if requested) */
2722         if (llss->check_dv1) {
2723                 rc = ll_data_version(llss->inode1, &dv, 0);
2724                 if (rc)
2725                         GOTO(putgl, rc);
2726                 if (dv != llss->dv1)
2727                         GOTO(putgl, rc = -EAGAIN);
2728         }
2729
2730         if (llss->check_dv2) {
2731                 rc = ll_data_version(llss->inode2, &dv, 0);
2732                 if (rc)
2733                         GOTO(putgl, rc);
2734                 if (dv != llss->dv2)
2735                         GOTO(putgl, rc = -EAGAIN);
2736         }
2737
2738         /* struct md_op_data is used to send the swap args to the mdt
2739          * only flags is missing, so we use struct mdc_swap_layouts
2740          * through the md_op_data->op_data */
2741         /* flags from user space have to be converted before they are send to
2742          * server, no flag is sent today, they are only used on the client */
2743         msl.msl_flags = 0;
2744         rc = -ENOMEM;
2745         op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2746                                      0, LUSTRE_OPC_ANY, &msl);
2747         if (IS_ERR(op_data))
2748                 GOTO(free, rc = PTR_ERR(op_data));
2749
2750         rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2751                            sizeof(*op_data), op_data, NULL);
2752         ll_finish_md_op_data(op_data);
2753
2754         if (rc < 0)
2755                 GOTO(putgl, rc);
2756
2757 putgl:
2758         if (gid != 0) {
2759                 ll_put_grouplock(llss->inode2, file2, gid);
2760                 ll_put_grouplock(llss->inode1, file1, gid);
2761         }
2762
2763 free:
2764         if (llss != NULL)
2765                 OBD_FREE_PTR(llss);
2766
2767         RETURN(rc);
2768 }
2769
2770 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
2771 {
2772         struct obd_export *exp = ll_i2mdexp(inode);
2773         struct md_op_data *op_data;
2774         int rc;
2775         ENTRY;
2776
2777         /* Detect out-of range masks */
2778         if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
2779                 RETURN(-EINVAL);
2780
2781         /* Non-root users are forbidden to set or clear flags which are
2782          * NOT defined in HSM_USER_MASK. */
2783         if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
2784             !cfs_capable(CFS_CAP_SYS_ADMIN))
2785                 RETURN(-EPERM);
2786
2787         if (!exp_connect_archive_id_array(exp)) {
2788                 /* Detect out-of range archive id */
2789                 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
2790                     (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
2791                         RETURN(-EINVAL);
2792         }
2793
2794         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2795                                      LUSTRE_OPC_ANY, hss);
2796         if (IS_ERR(op_data))
2797                 RETURN(PTR_ERR(op_data));
2798
2799         rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
2800                            op_data, NULL);
2801
2802         ll_finish_md_op_data(op_data);
2803
2804         RETURN(rc);
2805 }
2806
2807 static int ll_hsm_import(struct inode *inode, struct file *file,
2808                          struct hsm_user_import *hui)
2809 {
2810         struct hsm_state_set    *hss = NULL;
2811         struct iattr            *attr = NULL;
2812         int                      rc;
2813         ENTRY;
2814
2815         if (!S_ISREG(inode->i_mode))
2816                 RETURN(-EINVAL);
2817
2818         /* set HSM flags */
2819         OBD_ALLOC_PTR(hss);
2820         if (hss == NULL)
2821                 GOTO(out, rc = -ENOMEM);
2822
2823         hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
2824         hss->hss_archive_id = hui->hui_archive_id;
2825         hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
2826         rc = ll_hsm_state_set(inode, hss);
2827         if (rc != 0)
2828                 GOTO(out, rc);
2829
2830         OBD_ALLOC_PTR(attr);
2831         if (attr == NULL)
2832                 GOTO(out, rc = -ENOMEM);
2833
2834         attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
2835         attr->ia_mode |= S_IFREG;
2836         attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
2837         attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
2838         attr->ia_size = hui->hui_size;
2839         attr->ia_mtime.tv_sec = hui->hui_mtime;
2840         attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
2841         attr->ia_atime.tv_sec = hui->hui_atime;
2842         attr->ia_atime.tv_nsec = hui->hui_atime_ns;
2843
2844         attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
2845                          ATTR_UID | ATTR_GID |
2846                          ATTR_MTIME | ATTR_MTIME_SET |
2847                          ATTR_ATIME | ATTR_ATIME_SET;
2848
2849         inode_lock(inode);
2850
2851         rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
2852         if (rc == -ENODATA)
2853                 rc = 0;
2854
2855         inode_unlock(inode);
2856
2857 out:
2858         if (hss != NULL)
2859                 OBD_FREE_PTR(hss);
2860
2861         if (attr != NULL)
2862                 OBD_FREE_PTR(attr);
2863
2864         RETURN(rc);
2865 }
2866
2867 static inline long ll_lease_type_from_fmode(fmode_t fmode)
2868 {
2869         return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
2870                ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
2871 }
2872
2873 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
2874 {
2875         struct inode *inode = file_inode(file);
2876         struct iattr ia = {
2877                 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
2878                             ATTR_MTIME | ATTR_MTIME_SET |
2879                             ATTR_CTIME,
2880                 .ia_atime = {
2881                         .tv_sec = lfu->lfu_atime_sec,
2882                         .tv_nsec = lfu->lfu_atime_nsec,
2883                 },
2884                 .ia_mtime = {
2885                         .tv_sec = lfu->lfu_mtime_sec,
2886                         .tv_nsec = lfu->lfu_mtime_nsec,
2887                 },
2888                 .ia_ctime = {
2889                         .tv_sec = lfu->lfu_ctime_sec,
2890                         .tv_nsec = lfu->lfu_ctime_nsec,
2891                 },
2892         };
2893         int rc;
2894         ENTRY;
2895
2896         if (!capable(CAP_SYS_ADMIN))
2897                 RETURN(-EPERM);
2898
2899         if (!S_ISREG(inode->i_mode))
2900                 RETURN(-EINVAL);
2901
2902         inode_lock(inode);
2903         rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
2904                             false);
2905         inode_unlock(inode);
2906
2907         RETURN(rc);
2908 }
2909
2910 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
2911 {
2912         switch (mode) {
2913         case MODE_READ_USER:
2914                 return CLM_READ;
2915         case MODE_WRITE_USER:
2916                 return CLM_WRITE;
2917         default:
2918                 return -EINVAL;
2919         }
2920 }
2921
2922 static const char *const user_lockname[] = LOCK_MODE_NAMES;
2923
2924 /* Used to allow the upper layers of the client to request an LDLM lock
2925  * without doing an actual read or write.
2926  *
2927  * Used for ladvise lockahead to manually request specific locks.
2928  *
2929  * \param[in] file      file this ladvise lock request is on
2930  * \param[in] ladvise   ladvise struct describing this lock request
2931  *
2932  * \retval 0            success, no detailed result available (sync requests
2933  *                      and requests sent to the server [not handled locally]
2934  *                      cannot return detailed results)
2935  * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
2936  *                                       see definitions for details.
2937  * \retval negative     negative errno on error
2938  */
2939 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
2940 {
2941         struct lu_env *env = NULL;
2942         struct cl_io *io  = NULL;
2943         struct cl_lock *lock = NULL;
2944         struct cl_lock_descr *descr = NULL;
2945         struct dentry *dentry = file->f_path.dentry;
2946         struct inode *inode = dentry->d_inode;
2947         enum cl_lock_mode cl_mode;
2948         off_t start = ladvise->lla_start;
2949         off_t end = ladvise->lla_end;
2950         int result;
2951         __u16 refcheck;
2952
2953         ENTRY;
2954
2955         CDEBUG(D_VFSTRACE, "Lock request: file=%.*s, inode=%p, mode=%s "
2956                "start=%llu, end=%llu\n", dentry->d_name.len,
2957                dentry->d_name.name, dentry->d_inode,
2958                user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
2959                (__u64) end);
2960
2961         cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
2962         if (cl_mode < 0)
2963                 GOTO(out, result = cl_mode);
2964
2965         /* Get IO environment */
2966         result = cl_io_get(inode, &env, &io, &refcheck);
2967         if (result <= 0)
2968                 GOTO(out, result);
2969
2970         result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
2971         if (result > 0) {
2972                 /*
2973                  * nothing to do for this io. This currently happens when
2974                  * stripe sub-object's are not yet created.
2975                  */
2976                 result = io->ci_result;
2977         } else if (result == 0) {
2978                 lock = vvp_env_lock(env);
2979                 descr = &lock->cll_descr;
2980
2981                 descr->cld_obj   = io->ci_obj;
2982                 /* Convert byte offsets to pages */
2983                 descr->cld_start = cl_index(io->ci_obj, start);
2984                 descr->cld_end   = cl_index(io->ci_obj, end);
2985                 descr->cld_mode  = cl_mode;
2986                 /* CEF_MUST is used because we do not want to convert a
2987                  * lockahead request to a lockless lock */
2988                 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND |
2989                                        CEF_NONBLOCK;
2990
2991                 if (ladvise->lla_peradvice_flags & LF_ASYNC)
2992                         descr->cld_enq_flags |= CEF_SPECULATIVE;
2993
2994                 result = cl_lock_request(env, io, lock);
2995
2996                 /* On success, we need to release the lock */
2997                 if (result >= 0)
2998                         cl_lock_release(env, lock);
2999         }
3000         cl_io_fini(env, io);
3001         cl_env_put(env, &refcheck);
3002
3003         /* -ECANCELED indicates a matching lock with a different extent
3004          * was already present, and -EEXIST indicates a matching lock
3005          * on exactly the same extent was already present.
3006          * We convert them to positive values for userspace to make
3007          * recognizing true errors easier.
3008          * Note we can only return these detailed results on async requests,
3009          * as sync requests look the same as i/o requests for locking. */
3010         if (result == -ECANCELED)
3011                 result = LLA_RESULT_DIFFERENT;
3012         else if (result == -EEXIST)
3013                 result = LLA_RESULT_SAME;
3014
3015 out:
3016         RETURN(result);
3017 }
3018 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3019
3020 static int ll_ladvise_sanity(struct inode *inode,
3021                              struct llapi_lu_ladvise *ladvise)
3022 {
3023         struct ll_sb_info *sbi = ll_i2sbi(inode);
3024         enum lu_ladvise_type advice = ladvise->lla_advice;
3025         /* Note the peradvice flags is a 32 bit field, so per advice flags must
3026          * be in the first 32 bits of enum ladvise_flags */
3027         __u32 flags = ladvise->lla_peradvice_flags;
3028         /* 3 lines at 80 characters per line, should be plenty */
3029         int rc = 0;
3030
3031         if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3032                 rc = -EINVAL;
3033                 CDEBUG(D_VFSTRACE, "%s: advice with value '%d' not recognized,"
3034                        "last supported advice is %s (value '%d'): rc = %d\n",
3035                        sbi->ll_fsname, advice,
3036                        ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3037                 GOTO(out, rc);
3038         }
3039
3040         /* Per-advice checks */
3041         switch (advice) {
3042         case LU_LADVISE_LOCKNOEXPAND:
3043                 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3044                         rc = -EINVAL;
3045                         CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3046                                "rc = %d\n", sbi->ll_fsname, flags,
3047                                ladvise_names[advice], rc);
3048                         GOTO(out, rc);
3049                 }
3050                 break;
3051         case LU_LADVISE_LOCKAHEAD:
3052                 /* Currently only READ and WRITE modes can be requested */
3053                 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3054                     ladvise->lla_lockahead_mode == 0) {
3055                         rc = -EINVAL;
3056                         CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3057                                "rc = %d\n", sbi->ll_fsname,
3058                                ladvise->lla_lockahead_mode,
3059                                ladvise_names[advice], rc);
3060                         GOTO(out, rc);
3061                 }
3062                 /* fallthrough */
3063         case LU_LADVISE_WILLREAD:
3064         case LU_LADVISE_DONTNEED:
3065         default:
3066                 /* Note fall through above - These checks apply to all advices
3067                  * except LOCKNOEXPAND */
3068                 if (flags & ~LF_DEFAULT_MASK) {
3069                         rc = -EINVAL;
3070                         CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3071                                "rc = %d\n", sbi->ll_fsname, flags,
3072                                ladvise_names[advice], rc);
3073                         GOTO(out, rc);
3074                 }
3075                 if (ladvise->lla_start >= ladvise->lla_end) {
3076                         rc = -EINVAL;
3077                         CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3078                                "for %s: rc = %d\n", sbi->ll_fsname,
3079                                ladvise->lla_start, ladvise->lla_end,
3080                                ladvise_names[advice], rc);
3081                         GOTO(out, rc);
3082                 }
3083                 break;
3084         }
3085
3086 out:
3087         return rc;
3088 }
3089 #undef ERRSIZE
3090
3091 /*
3092  * Give file access advices
3093  *
3094  * The ladvise interface is similar to Linux fadvise() system call, except it
3095  * forwards the advices directly from Lustre client to server. The server side
3096  * codes will apply appropriate read-ahead and caching techniques for the
3097  * corresponding files.
3098  *
3099  * A typical workload for ladvise is e.g. a bunch of different clients are
3100  * doing small random reads of a file, so prefetching pages into OSS cache
3101  * with big linear reads before the random IO is a net benefit. Fetching
3102  * all that data into each client cache with fadvise() may not be, due to
3103  * much more data being sent to the client.
3104  */
3105 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3106                       struct llapi_lu_ladvise *ladvise)
3107 {
3108         struct lu_env *env;
3109         struct cl_io *io;
3110         struct cl_ladvise_io *lio;
3111         int rc;
3112         __u16 refcheck;
3113         ENTRY;
3114
3115         env = cl_env_get(&refcheck);
3116         if (IS_ERR(env))
3117                 RETURN(PTR_ERR(env));
3118
3119         io = vvp_env_thread_io(env);
3120         io->ci_obj = ll_i2info(inode)->lli_clob;
3121
3122         /* initialize parameters for ladvise */
3123         lio = &io->u.ci_ladvise;
3124         lio->li_start = ladvise->lla_start;
3125         lio->li_end = ladvise->lla_end;
3126         lio->li_fid = ll_inode2fid(inode);
3127         lio->li_advice = ladvise->lla_advice;
3128         lio->li_flags = flags;
3129
3130         if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3131                 rc = cl_io_loop(env, io);
3132         else
3133                 rc = io->ci_result;
3134
3135         cl_io_fini(env, io);
3136         cl_env_put(env, &refcheck);
3137         RETURN(rc);
3138 }
3139
3140 static int ll_lock_noexpand(struct file *file, int flags)
3141 {
3142         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
3143
3144         fd->ll_lock_no_expand = !(flags & LF_UNSET);
3145
3146         return 0;
3147 }
3148
3149 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3150                         unsigned long arg)
3151 {
3152         struct fsxattr fsxattr;
3153
3154         if (copy_from_user(&fsxattr,
3155                            (const struct fsxattr __user *)arg,
3156                            sizeof(fsxattr)))
3157                 RETURN(-EFAULT);
3158
3159         fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3160         if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
3161                 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3162         fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3163         if (copy_to_user((struct fsxattr __user *)arg,
3164                          &fsxattr, sizeof(fsxattr)))
3165                 RETURN(-EFAULT);
3166
3167         RETURN(0);
3168 }
3169
3170 int ll_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
3171 {
3172         /*
3173          * Project Quota ID state is only allowed to change from within the init
3174          * namespace. Enforce that restriction only if we are trying to change
3175          * the quota ID state. Everything else is allowed in user namespaces.
3176          */
3177         if (current_user_ns() == &init_user_ns)
3178                 return 0;
3179
3180         if (ll_i2info(inode)->lli_projid != fa->fsx_projid)
3181                 return -EINVAL;
3182
3183         if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT)) {
3184                 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
3185                         return -EINVAL;
3186         } else {
3187                 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
3188                         return -EINVAL;
3189         }
3190
3191         return 0;
3192 }
3193
3194 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3195                         unsigned long arg)
3196 {
3197
3198         struct md_op_data *op_data;
3199         struct ptlrpc_request *req = NULL;
3200         int rc = 0;
3201         struct fsxattr fsxattr;
3202         struct cl_object *obj;
3203         struct iattr *attr;
3204         int flags;
3205
3206         if (copy_from_user(&fsxattr,
3207                            (const struct fsxattr __user *)arg,
3208                            sizeof(fsxattr)))
3209                 RETURN(-EFAULT);
3210
3211         rc = ll_ioctl_check_project(inode, &fsxattr);
3212         if (rc)
3213                 RETURN(rc);
3214
3215         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3216                                      LUSTRE_OPC_ANY, NULL);
3217         if (IS_ERR(op_data))
3218                 RETURN(PTR_ERR(op_data));
3219
3220         flags = ll_xflags_to_inode_flags(fsxattr.fsx_xflags);
3221         op_data->op_attr_flags = ll_inode_to_ext_flags(flags);
3222         if (fsxattr.fsx_xflags & FS_XFLAG_PROJINHERIT)
3223                 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3224         op_data->op_projid = fsxattr.fsx_projid;
3225         op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3226         rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL,
3227                         0, &req);
3228         ptlrpc_req_finished(req);
3229         if (rc)
3230                 GOTO(out_fsxattr, rc);
3231         ll_update_inode_flags(inode, op_data->op_attr_flags);
3232         obj = ll_i2info(inode)->lli_clob;
3233         if (obj == NULL)
3234                 GOTO(out_fsxattr, rc);
3235
3236         OBD_ALLOC_PTR(attr);
3237         if (attr == NULL)
3238                 GOTO(out_fsxattr, rc = -ENOMEM);
3239
3240         rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS,
3241                             fsxattr.fsx_xflags);
3242         OBD_FREE_PTR(attr);
3243 out_fsxattr:
3244         ll_finish_md_op_data(op_data);
3245         RETURN(rc);
3246 }
3247
3248 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3249                                  unsigned long arg)
3250 {
3251         struct inode            *inode = file_inode(file);
3252         struct ll_file_data     *fd = LUSTRE_FPRIVATE(file);
3253         struct ll_inode_info    *lli = ll_i2info(inode);
3254         struct obd_client_handle *och = NULL;
3255         struct split_param sp;
3256         struct pcc_param param;
3257         bool lease_broken = false;
3258         fmode_t fmode = 0;
3259         enum mds_op_bias bias = 0;
3260         struct file *layout_file = NULL;
3261         void *data = NULL;
3262         size_t data_size = 0;
3263         bool attached = false;
3264         long rc, rc2 = 0;
3265
3266         ENTRY;
3267
3268         mutex_lock(&lli->lli_och_mutex);
3269         if (fd->fd_lease_och != NULL) {
3270                 och = fd->fd_lease_och;
3271                 fd->fd_lease_och = NULL;
3272         }
3273         mutex_unlock(&lli->lli_och_mutex);
3274
3275         if (och == NULL)
3276                 RETURN(-ENOLCK);
3277
3278         fmode = och->och_flags;
3279
3280         switch (ioc->lil_flags) {
3281         case LL_LEASE_RESYNC_DONE:
3282                 if (ioc->lil_count > IOC_IDS_MAX)
3283                         GOTO(out_lease_close, rc = -EINVAL);
3284
3285                 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3286                 OBD_ALLOC(data, data_size);
3287                 if (!data)
3288                         GOTO(out_lease_close, rc = -ENOMEM);
3289
3290                 if (copy_from_user(data, (void __user *)arg, data_size))
3291                         GOTO(out_lease_close, rc = -EFAULT);
3292
3293                 bias = MDS_CLOSE_RESYNC_DONE;
3294                 break;
3295         case LL_LEASE_LAYOUT_MERGE: {
3296                 int fd;
3297
3298                 if (ioc->lil_count != 1)
3299                         GOTO(out_lease_close, rc = -EINVAL);
3300
3301                 arg += sizeof(*ioc);
3302                 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3303                         GOTO(out_lease_close, rc = -EFAULT);
3304
3305                 layout_file = fget(fd);
3306                 if (!layout_file)
3307                         GOTO(out_lease_close, rc = -EBADF);
3308
3309                 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3310                                 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3311                         GOTO(out_lease_close, rc = -EPERM);
3312
3313                 data = file_inode(layout_file);
3314                 bias = MDS_CLOSE_LAYOUT_MERGE;
3315                 break;
3316         }
3317         case LL_LEASE_LAYOUT_SPLIT: {
3318                 int fdv;
3319                 int mirror_id;
3320
3321                 if (ioc->lil_count != 2)
3322                         GOTO(out_lease_close, rc = -EINVAL);
3323
3324                 arg += sizeof(*ioc);
3325                 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3326                         GOTO(out_lease_close, rc = -EFAULT);
3327
3328                 arg += sizeof(__u32);
3329                 if (copy_from_user(&mirror_id, (void __user *)arg,
3330                                    sizeof(__u32)))
3331                         GOTO(out_lease_close, rc = -EFAULT);
3332
3333                 layout_file = fget(fdv);
3334                 if (!layout_file)
3335                         GOTO(out_lease_close, rc = -EBADF);
3336
3337                 sp.sp_inode = file_inode(layout_file);
3338                 sp.sp_mirror_id = (__u16)mirror_id;
3339                 data = &sp;
3340                 bias = MDS_CLOSE_LAYOUT_SPLIT;
3341                 break;
3342         }
3343         case LL_LEASE_PCC_ATTACH:
3344                 if (ioc->lil_count != 1)
3345                         RETURN(-EINVAL);
3346
3347                 arg += sizeof(*ioc);
3348                 if (copy_from_user(&param.pa_archive_id, (void __user *)arg,
3349                                    sizeof(__u32)))
3350                         GOTO(out_lease_close, rc2 = -EFAULT);
3351
3352                 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3353                 if (rc2)
3354                         GOTO(out_lease_close, rc2);
3355
3356                 attached = true;
3357                 /* Grab latest data version */
3358                 rc2 = ll_data_version(inode, &param.pa_data_version,
3359                                      LL_DV_WR_FLUSH);
3360                 if (rc2)
3361                         GOTO(out_lease_close, rc2);
3362
3363                 data = &param;
3364                 bias = MDS_PCC_ATTACH;
3365                 break;
3366         default:
3367                 /* without close intent */
3368                 break;
3369         }
3370
3371 out_lease_close:
3372         rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3373         if (rc < 0)
3374                 GOTO(out, rc);
3375
3376         rc = ll_lease_och_release(inode, file);
3377         if (rc < 0)
3378                 GOTO(out, rc);
3379
3380         if (lease_broken)
3381                 fmode = 0;
3382         EXIT;
3383
3384 out:
3385         switch (ioc->lil_flags) {
3386         case LL_LEASE_RESYNC_DONE:
3387                 if (data)
3388                         OBD_FREE(data, data_size);
3389                 break;
3390         case LL_LEASE_LAYOUT_MERGE:
3391         case LL_LEASE_LAYOUT_SPLIT:
3392                 if (layout_file)
3393                         fput(layout_file);
3394                 break;
3395         case LL_LEASE_PCC_ATTACH:
3396                 if (!rc)
3397                         rc = rc2;
3398                 rc = pcc_readwrite_attach_fini(file, inode,
3399                                                param.pa_layout_gen,
3400                                                lease_broken, rc,
3401                                                attached);
3402                 break;
3403         }
3404
3405         if (!rc)
3406                 rc = ll_lease_type_from_fmode(fmode);
3407         RETURN(rc);
3408 }
3409
3410 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3411                               unsigned long arg)
3412 {
3413         struct inode *inode = file_inode(file);
3414         struct ll_inode_info *lli = ll_i2info(inode);
3415         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
3416         struct obd_client_handle *och = NULL;
3417         __u64 open_flags = 0;
3418         bool lease_broken;
3419         fmode_t fmode;
3420         long rc;
3421         ENTRY;
3422
3423         switch (ioc->lil_mode) {
3424         case LL_LEASE_WRLCK:
3425                 if (!(file->f_mode & FMODE_WRITE))
3426                         RETURN(-EPERM);
3427                 fmode = FMODE_WRITE;
3428                 break;
3429         case LL_LEASE_RDLCK:
3430                 if (!(file->f_mode & FMODE_READ))
3431                         RETURN(-EPERM);
3432                 fmode = FMODE_READ;
3433                 break;
3434         case LL_LEASE_UNLCK:
3435                 RETURN(ll_file_unlock_lease(file, ioc, arg));
3436         default:
3437                 RETURN(-EINVAL);
3438         }
3439
3440         CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3441
3442         /* apply for lease */
3443         if (ioc->lil_flags & LL_LEASE_RESYNC)
3444                 open_flags = MDS_OPEN_RESYNC;
3445         och = ll_lease_open(inode, file, fmode, open_flags);
3446         if (IS_ERR(och))
3447                 RETURN(PTR_ERR(och));
3448
3449         if (ioc->lil_flags & LL_LEASE_RESYNC) {
3450                 rc = ll_lease_file_resync(och, inode, arg);
3451                 if (rc) {
3452                         ll_lease_close(och, inode, NULL);
3453                         RETURN(rc);
3454                 }
3455                 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3456                 if (rc) {
3457                         ll_lease_close(och, inode, NULL);
3458                         RETURN(rc);
3459                 }
3460         }
3461
3462         rc = 0;
3463         mutex_lock(&lli->lli_och_mutex);
3464         if (fd->fd_lease_och == NULL) {
3465                 fd->fd_lease_och = och;
3466                 och = NULL;
3467         }
3468         mutex_unlock(&lli->lli_och_mutex);
3469         if (och != NULL) {
3470                 /* impossible now that only excl is supported for now */
3471                 ll_lease_close(och, inode, &lease_broken);
3472                 rc = -EBUSY;
3473         }
3474         RETURN(rc);
3475 }
3476
3477 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3478 {
3479         struct ll_inode_info *lli = ll_i2info(inode);
3480         struct ll_sb_info *sbi = ll_i2sbi(inode);
3481         __u64 now = ktime_get_real_seconds();
3482         int i;
3483
3484         spin_lock(&lli->lli_heat_lock);
3485         heat->lh_flags = lli->lli_heat_flags;
3486         for (i = 0; i < heat->lh_count; i++)
3487                 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3488                                                 now, sbi->ll_heat_decay_weight,
3489                                                 sbi->ll_heat_period_second);
3490         spin_unlock(&lli->lli_heat_lock);
3491 }
3492
3493 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3494 {
3495         struct ll_inode_info *lli = ll_i2info(inode);
3496         int rc = 0;
3497
3498         spin_lock(&lli->lli_heat_lock);
3499         if (flags & LU_HEAT_FLAG_CLEAR)
3500                 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3501
3502         if (flags & LU_HEAT_FLAG_OFF)
3503                 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3504         else
3505                 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3506
3507         spin_unlock(&lli->lli_heat_lock);
3508
3509         RETURN(rc);
3510 }
3511
3512 static long
3513 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3514 {
3515         struct inode            *inode = file_inode(file);
3516         struct ll_file_data     *fd = LUSTRE_FPRIVATE(file);
3517         int                      flags, rc;
3518         ENTRY;
3519
3520         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3521                PFID(ll_inode2fid(inode)), inode, cmd);
3522         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3523
3524         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3525         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3526                 RETURN(-ENOTTY);
3527
3528         switch (cmd) {
3529         case LL_IOC_GETFLAGS:
3530                 /* Get the current value of the file flags */
3531                 return put_user(fd->fd_flags, (int __user *)arg);
3532         case LL_IOC_SETFLAGS:
3533         case LL_IOC_CLRFLAGS:
3534                 /* Set or clear specific file flags */
3535                 /* XXX This probably needs checks to ensure the flags are
3536                  *     not abused, and to handle any flag side effects.
3537                  */
3538                 if (get_user(flags, (int __user *) arg))
3539                         RETURN(-EFAULT);
3540
3541                 if (cmd == LL_IOC_SETFLAGS) {
3542                         if ((flags & LL_FILE_IGNORE_LOCK) &&
3543                             !(file->f_flags & O_DIRECT)) {
3544                                 CERROR("%s: unable to disable locking on "
3545                                        "non-O_DIRECT file\n", current->comm);
3546                                 RETURN(-EINVAL);
3547                         }
3548
3549                         fd->fd_flags |= flags;
3550                 } else {
3551                         fd->fd_flags &= ~flags;
3552                 }
3553                 RETURN(0);
3554         case LL_IOC_LOV_SETSTRIPE:
3555         case LL_IOC_LOV_SETSTRIPE_NEW:
3556                 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3557         case LL_IOC_LOV_SETEA:
3558                 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3559         case LL_IOC_LOV_SWAP_LAYOUTS: {
3560                 struct file *file2;
3561                 struct lustre_swap_layouts lsl;
3562
3563                 if (copy_from_user(&lsl, (char __user *)arg,
3564                                    sizeof(struct lustre_swap_layouts)))
3565                         RETURN(-EFAULT);
3566
3567                 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3568                         RETURN(-EPERM);
3569
3570                 file2 = fget(lsl.sl_fd);
3571                 if (file2 == NULL)
3572                         RETURN(-EBADF);
3573
3574                 /* O_WRONLY or O_RDWR */
3575                 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3576                         GOTO(out, rc = -EPERM);
3577
3578                 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3579                         struct inode                    *inode2;
3580                         struct ll_inode_info            *lli;
3581                         struct obd_client_handle        *och = NULL;
3582
3583                         lli = ll_i2info(inode);
3584                         mutex_lock(&lli->lli_och_mutex);
3585                         if (fd->fd_lease_och != NULL) {
3586                                 och = fd->fd_lease_och;
3587                                 fd->fd_lease_och = NULL;
3588                         }
3589                         mutex_unlock(&lli->lli_och_mutex);
3590                         if (och == NULL)
3591                                 GOTO(out, rc = -ENOLCK);
3592                         inode2 = file_inode(file2);
3593                         rc = ll_swap_layouts_close(och, inode, inode2);
3594                 } else {
3595                         rc = ll_swap_layouts(file, file2, &lsl);
3596                 }
3597 out:
3598                 fput(file2);
3599                 RETURN(rc);
3600         }
3601         case LL_IOC_LOV_GETSTRIPE:
3602         case LL_IOC_LOV_GETSTRIPE_NEW:
3603                 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3604         case FS_IOC_GETFLAGS:
3605         case FS_IOC_SETFLAGS:
3606                 RETURN(ll_iocontrol(inode, file, cmd, arg));
3607         case FSFILT_IOC_GETVERSION:
3608         case FS_IOC_GETVERSION:
3609                 RETURN(put_user(inode->i_generation, (int __user *)arg));
3610         /* We need to special case any other ioctls we want to handle,
3611          * to send them to the MDS/OST as appropriate and to properly
3612          * network encode the arg field. */
3613         case FS_IOC_SETVERSION:
3614                 RETURN(-ENOTSUPP);
3615
3616         case LL_IOC_GROUP_LOCK:
3617                 RETURN(ll_get_grouplock(inode, file, arg));
3618         case LL_IOC_GROUP_UNLOCK:
3619                 RETURN(ll_put_grouplock(inode, file, arg));
3620         case IOC_OBD_STATFS:
3621                 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3622
3623         case LL_IOC_FLUSHCTX:
3624                 RETURN(ll_flush_ctx(inode));
3625         case LL_IOC_PATH2FID: {
3626                 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3627                                  sizeof(struct lu_fid)))
3628                         RETURN(-EFAULT);
3629
3630                 RETURN(0);
3631         }
3632         case LL_IOC_GETPARENT:
3633                 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3634
3635         case OBD_IOC_FID2PATH:
3636                 RETURN(ll_fid2path(inode, (void __user *)arg));
3637         case LL_IOC_DATA_VERSION: {
3638                 struct ioc_data_version idv;
3639                 int rc;
3640
3641                 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3642                         RETURN(-EFAULT);
3643
3644                 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3645                 rc = ll_ioc_data_version(inode, &idv);
3646
3647                 if (rc == 0 &&
3648                     copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3649                         RETURN(-EFAULT);
3650
3651                 RETURN(rc);
3652         }
3653
3654         case LL_IOC_GET_MDTIDX: {
3655                 int mdtidx;
3656
3657                 mdtidx = ll_get_mdt_idx(inode);
3658                 if (mdtidx < 0)
3659                         RETURN(mdtidx);
3660
3661                 if (put_user((int)mdtidx, (int __user *)arg))
3662                         RETURN(-EFAULT);
3663
3664                 RETURN(0);
3665         }
3666         case OBD_IOC_GETDTNAME:
3667         case OBD_IOC_GETMDNAME:
3668                 RETURN(ll_get_obd_name(inode, cmd, arg));
3669         case LL_IOC_HSM_STATE_GET: {
3670                 struct md_op_data       *op_data;
3671                 struct hsm_user_state   *hus;
3672                 int                      rc;
3673
3674                 OBD_ALLOC_PTR(hus);
3675                 if (hus == NULL)
3676                         RETURN(-ENOMEM);
3677
3678                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3679                                              LUSTRE_OPC_ANY, hus);
3680                 if (IS_ERR(op_data)) {
3681                         OBD_FREE_PTR(hus);
3682                         RETURN(PTR_ERR(op_data));
3683                 }
3684
3685                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3686                                    op_data, NULL);
3687
3688                 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
3689                         rc = -EFAULT;
3690
3691                 ll_finish_md_op_data(op_data);
3692                 OBD_FREE_PTR(hus);
3693                 RETURN(rc);
3694         }
3695         case LL_IOC_HSM_STATE_SET: {
3696                 struct hsm_state_set    *hss;
3697                 int                      rc;
3698
3699                 OBD_ALLOC_PTR(hss);
3700                 if (hss == NULL)
3701                         RETURN(-ENOMEM);
3702
3703                 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
3704                         OBD_FREE_PTR(hss);
3705                         RETURN(-EFAULT);
3706                 }
3707
3708                 rc = ll_hsm_state_set(inode, hss);
3709
3710                 OBD_FREE_PTR(hss);
3711                 RETURN(rc);
3712         }
3713         case LL_IOC_HSM_ACTION: {
3714                 struct md_op_data               *op_data;
3715                 struct hsm_current_action       *hca;
3716                 int                              rc;
3717
3718                 OBD_ALLOC_PTR(hca);
3719                 if (hca == NULL)
3720                         RETURN(-ENOMEM);
3721
3722                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3723                                              LUSTRE_OPC_ANY, hca);
3724                 if (IS_ERR(op_data)) {
3725                         OBD_FREE_PTR(hca);
3726                         RETURN(PTR_ERR(op_data));
3727                 }
3728
3729                 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3730                                    op_data, NULL);
3731
3732                 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
3733                         rc = -EFAULT;
3734
3735                 ll_finish_md_op_data(op_data);
3736                 OBD_FREE_PTR(hca);
3737                 RETURN(rc);
3738         }
3739         case LL_IOC_SET_LEASE_OLD: {
3740                 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
3741
3742                 RETURN(ll_file_set_lease(file, &ioc, 0));
3743         }
3744         case LL_IOC_SET_LEASE: {
3745                 struct ll_ioc_lease ioc;
3746
3747                 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
3748                         RETURN(-EFAULT);
3749
3750                 RETURN(ll_file_set_lease(file, &ioc, arg));
3751         }
3752         case LL_IOC_GET_LEASE: {
3753                 struct ll_inode_info *lli = ll_i2info(inode);
3754                 struct ldlm_lock *lock = NULL;
3755                 fmode_t fmode = 0;
3756
3757                 mutex_lock(&lli->lli_och_mutex);
3758                 if (fd->fd_lease_och != NULL) {
3759                         struct obd_client_handle *och = fd->fd_lease_och;
3760
3761                         lock = ldlm_handle2lock(&och->och_lease_handle);
3762                         if (lock != NULL) {
3763                                 lock_res_and_lock(lock);
3764                                 if (!ldlm_is_cancel(lock))
3765                                         fmode = och->och_flags;
3766
3767                                 unlock_res_and_lock(lock);
3768                                 LDLM_LOCK_PUT(lock);
3769                         }
3770                 }
3771                 mutex_unlock(&lli->lli_och_mutex);
3772
3773                 RETURN(ll_lease_type_from_fmode(fmode));
3774         }
3775         case LL_IOC_HSM_IMPORT: {
3776                 struct hsm_user_import *hui;
3777
3778                 OBD_ALLOC_PTR(hui);
3779                 if (hui == NULL)
3780                         RETURN(-ENOMEM);
3781
3782                 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
3783                         OBD_FREE_PTR(hui);
3784                         RETURN(-EFAULT);
3785                 }
3786
3787                 rc = ll_hsm_import(inode, file, hui);
3788
3789                 OBD_FREE_PTR(hui);
3790                 RETURN(rc);
3791         }
3792         case LL_IOC_FUTIMES_3: {
3793                 struct ll_futimes_3 lfu;
3794
3795                 if (copy_from_user(&lfu,
3796                                    (const struct ll_futimes_3 __user *)arg,
3797                                    sizeof(lfu)))
3798                         RETURN(-EFAULT);
3799
3800                 RETURN(ll_file_futimes_3(file, &lfu));
3801         }
3802         case LL_IOC_LADVISE: {
3803                 struct llapi_ladvise_hdr *k_ladvise_hdr;
3804                 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
3805                 int i;
3806                 int num_advise;
3807                 int alloc_size = sizeof(*k_ladvise_hdr);
3808
3809                 rc = 0;
3810                 u_ladvise_hdr = (void __user *)arg;
3811                 OBD_ALLOC_PTR(k_ladvise_hdr);
3812                 if (k_ladvise_hdr == NULL)
3813                         RETURN(-ENOMEM);
3814
3815                 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3816                         GOTO(out_ladvise, rc = -EFAULT);
3817
3818                 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
3819                     k_ladvise_hdr->lah_count < 1)
3820                         GOTO(out_ladvise, rc = -EINVAL);
3821
3822                 num_advise = k_ladvise_hdr->lah_count;
3823                 if (num_advise >= LAH_COUNT_MAX)
3824                         GOTO(out_ladvise, rc = -EFBIG);
3825
3826                 OBD_FREE_PTR(k_ladvise_hdr);
3827                 alloc_size = offsetof(typeof(*k_ladvise_hdr),
3828                                       lah_advise[num_advise]);
3829                 OBD_ALLOC(k_ladvise_hdr, alloc_size);
3830                 if (k_ladvise_hdr == NULL)
3831                         RETURN(-ENOMEM);
3832
3833                 /*
3834                  * TODO: submit multiple advices to one server in a single RPC
3835                  */
3836                 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3837                         GOTO(out_ladvise, rc = -EFAULT);
3838
3839                 for (i = 0; i < num_advise; i++) {
3840                         struct llapi_lu_ladvise *k_ladvise =
3841                                         &k_ladvise_hdr->lah_advise[i];
3842                         struct llapi_lu_ladvise __user *u_ladvise =
3843                                         &u_ladvise_hdr->lah_advise[i];
3844
3845                         rc = ll_ladvise_sanity(inode, k_ladvise);
3846                         if (rc)
3847                                 GOTO(out_ladvise, rc);
3848
3849                         switch (k_ladvise->lla_advice) {
3850                         case LU_LADVISE_LOCKNOEXPAND:
3851                                 rc = ll_lock_noexpand(file,
3852                                                k_ladvise->lla_peradvice_flags);
3853                                 GOTO(out_ladvise, rc);
3854                         case LU_LADVISE_LOCKAHEAD:
3855
3856                                 rc = ll_file_lock_ahead(file, k_ladvise);
3857
3858                                 if (rc < 0)
3859                                         GOTO(out_ladvise, rc);
3860
3861                                 if (put_user(rc,
3862                                              &u_ladvise->lla_lockahead_result))
3863                                         GOTO(out_ladvise, rc = -EFAULT);
3864                                 break;
3865                         default:
3866                                 rc = ll_ladvise(inode, file,
3867                                                 k_ladvise_hdr->lah_flags,
3868                                                 k_ladvise);
3869                                 if (rc)
3870                                         GOTO(out_ladvise, rc);
3871                                 break;
3872                         }
3873
3874                 }
3875
3876 out_ladvise:
3877                 OBD_FREE(k_ladvise_hdr, alloc_size);
3878                 RETURN(rc);
3879         }
3880         case LL_IOC_FLR_SET_MIRROR: {
3881                 /* mirror I/O must be direct to avoid polluting page cache
3882                  * by stale data. */
3883                 if (!(file->f_flags & O_DIRECT))
3884                         RETURN(-EINVAL);
3885
3886                 fd->fd_designated_mirror = (__u32)arg;
3887                 RETURN(0);
3888         }
3889         case LL_IOC_FSGETXATTR:
3890                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
3891         case LL_IOC_FSSETXATTR:
3892                 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
3893         case BLKSSZGET:
3894                 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
3895         case LL_IOC_HEAT_GET: {
3896                 struct lu_heat uheat;
3897                 struct lu_heat *heat;
3898                 int size;
3899
3900                 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
3901                         RETURN(-EFAULT);
3902
3903                 if (uheat.lh_count > OBD_HEAT_COUNT)
3904                         uheat.lh_count = OBD_HEAT_COUNT;
3905
3906                 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
3907                 OBD_ALLOC(heat, size);
3908                 if (heat == NULL)
3909                         RETURN(-ENOMEM);
3910
3911                 heat->lh_count = uheat.lh_count;
3912                 ll_heat_get(inode, heat);
3913                 rc = copy_to_user((char __user *)arg, heat, size);
3914                 OBD_FREE(heat, size);
3915                 RETURN(rc ? -EFAULT : 0);
3916         }
3917         case LL_IOC_HEAT_SET: {
3918                 __u64 flags;
3919
3920                 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
3921                         RETURN(-EFAULT);
3922
3923                 rc = ll_heat_set(inode, flags);
3924                 RETURN(rc);
3925         }
3926         case LL_IOC_PCC_DETACH: {
3927                 struct lu_pcc_detach *detach;
3928
3929                 OBD_ALLOC_PTR(detach);
3930                 if (detach == NULL)
3931                         RETURN(-ENOMEM);
3932
3933                 if (copy_from_user(detach,
3934                                    (const struct lu_pcc_detach __user *)arg,
3935                                    sizeof(*detach)))
3936                         GOTO(out_detach_free, rc = -EFAULT);
3937
3938                 if (!S_ISREG(inode->i_mode))
3939                         GOTO(out_detach_free, rc = -EINVAL);
3940
3941                 if (!inode_owner_or_capable(inode))
3942                         GOTO(out_detach_free, rc = -EPERM);
3943
3944                 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
3945 out_detach_free:
3946                 OBD_FREE_PTR(detach);
3947                 RETURN(rc);
3948         }
3949         case LL_IOC_PCC_STATE: {
3950                 struct lu_pcc_state __user *ustate =
3951                         (struct lu_pcc_state __user *)arg;
3952                 struct lu_pcc_state *state;
3953
3954                 OBD_ALLOC_PTR(state);
3955                 if (state == NULL)
3956                         RETURN(-ENOMEM);
3957
3958                 if (copy_from_user(state, ustate, sizeof(*state)))
3959                         GOTO(out_state, rc = -EFAULT);
3960
3961                 rc = pcc_ioctl_state(file, inode, state);
3962                 if (rc)
3963                         GOTO(out_state, rc);
3964
3965                 if (copy_to_user(ustate, state, sizeof(*state)))
3966                         GOTO(out_state, rc = -EFAULT);
3967
3968 out_state:
3969                 OBD_FREE_PTR(state);
3970                 RETURN(rc);
3971         }
3972         default:
3973                 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
3974                                      (void __user *)arg));
3975         }
3976 }
3977
3978 #ifndef HAVE_FILE_LLSEEK_SIZE
3979 static inline loff_t
3980 llseek_execute(struct file *file, loff_t offset, loff_t maxsize)
3981 {
3982         if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
3983                 return -EINVAL;
3984         if (offset > maxsize)
3985                 return -EINVAL;
3986
3987         if (offset != file->f_pos) {
3988                 file->f_pos = offset;
3989                 file->f_version = 0;
3990         }
3991         return offset;
3992 }
3993
3994 static loff_t
3995 generic_file_llseek_size(struct file *file, loff_t offset, int origin,
3996                 loff_t maxsize, loff_t eof)
3997 {
3998         struct inode *inode = file_inode(file);
3999
4000         switch (origin) {
4001         case SEEK_END:
4002                 offset += eof;
4003                 break;
4004         case SEEK_CUR:
4005                 /*
4006                  * Here we special-case the lseek(fd, 0, SEEK_CUR)
4007                  * position-querying operation.  Avoid rewriting the "same"
4008                  * f_pos value back to the file because a concurrent read(),
4009                  * write() or lseek() might have altered it
4010                  */
4011                 if (offset == 0)
4012                         return file->f_pos;
4013                 /*
4014                  * f_lock protects against read/modify/write race with other
4015                  * SEEK_CURs. Note that parallel writes and reads behave
4016                  * like SEEK_SET.
4017                  */
4018                 inode_lock(inode);
4019                 offset = llseek_execute(file, file->f_pos + offset, maxsize);
4020                 inode_unlock(inode);
4021                 return offset;
4022         case SEEK_DATA:
4023                 /*
4024                  * In the generic case the entire file is data, so as long as
4025                  * offset isn't at the end of the file then the offset is data.
4026                  */
4027                 if (offset >= eof)
4028                         return -ENXIO;
4029                 break;
4030         case SEEK_HOLE:
4031                 /*
4032                  * There is a virtual hole at the end of the file, so as long as
4033                  * offset isn't i_size or larger, return i_size.
4034                  */
4035                 if (offset >= eof)
4036                         return -ENXIO;
4037                 offset = eof;
4038                 break;
4039         }
4040
4041         return llseek_execute(file, offset, maxsize);
4042 }
4043 #endif
4044
4045 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4046 {
4047         struct inode *inode = file_inode(file);
4048         loff_t retval, eof = 0;
4049         ktime_t kstart = ktime_get();
4050
4051         ENTRY;
4052         retval = offset + ((origin == SEEK_END) ? i_size_read(inode) :
4053                            (origin == SEEK_CUR) ? file->f_pos : 0);
4054         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4055                PFID(ll_inode2fid(inode)), inode, retval, retval,
4056                origin);
4057
4058         if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) {
4059                 retval = ll_glimpse_size(inode);
4060                 if (retval != 0)
4061                         RETURN(retval);
4062                 eof = i_size_read(inode);
4063         }
4064
4065         retval = ll_generic_file_llseek_size(file, offset, origin,
4066                                              ll_file_maxbytes(inode), eof);
4067         if (retval >= 0)
4068                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4069                                    ktime_us_delta(ktime_get(), kstart));
4070         RETURN(retval);
4071 }
4072
4073 static int ll_flush(struct file *file, fl_owner_t id)
4074 {
4075         struct inode *inode = file_inode(file);
4076         struct ll_inode_info *lli = ll_i2info(inode);
4077         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
4078         int rc, err;
4079
4080         LASSERT(!S_ISDIR(inode->i_mode));
4081
4082         /* catch async errors that were recorded back when async writeback
4083          * failed for pages in this mapping. */
4084         rc = lli->lli_async_rc;
4085         lli->lli_async_rc = 0;
4086         if (lli->lli_clob != NULL) {
4087                 err = lov_read_and_clear_async_rc(lli->lli_clob);
4088                 if (rc == 0)
4089                         rc = err;
4090         }
4091
4092         /* The application has been told write failure already.
4093          * Do not report failure again. */
4094         if (fd->fd_write_failed)
4095                 return 0;
4096         return rc ? -EIO : 0;
4097 }
4098
4099 /**
4100  * Called to make sure a portion of file has been written out.
4101  * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4102  *
4103  * Return how many pages have been written.
4104  */
4105 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4106                        enum cl_fsync_mode mode, int ignore_layout)
4107 {
4108         struct lu_env *env;
4109         struct cl_io *io;
4110         struct cl_fsync_io *fio;
4111         int result;
4112         __u16 refcheck;
4113         ENTRY;
4114
4115         if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4116             mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4117                 RETURN(-EINVAL);
4118
4119         env = cl_env_get(&refcheck);
4120         if (IS_ERR(env))
4121                 RETURN(PTR_ERR(env));
4122
4123         io = vvp_env_thread_io(env);
4124         io->ci_obj = ll_i2info(inode)->lli_clob;
4125         io->ci_ignore_layout = ignore_layout;
4126
4127         /* initialize parameters for sync */
4128         fio = &io->u.ci_fsync;
4129         fio->fi_start = start;
4130         fio->fi_end = end;
4131         fio->fi_fid = ll_inode2fid(inode);
4132         fio->fi_mode = mode;
4133         fio->fi_nr_written = 0;
4134
4135         if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4136                 result = cl_io_loop(env, io);
4137         else
4138                 result = io->ci_result;
4139         if (result == 0)
4140                 result = fio->fi_nr_written;
4141         cl_io_fini(env, io);
4142         cl_env_put(env, &refcheck);
4143
4144         RETURN(result);
4145 }
4146
4147 /*
4148  * When dentry is provided (the 'else' case), file_dentry() may be
4149  * null and dentry must be used directly rather than pulled from
4150  * file_dentry() as is done otherwise.
4151  */
4152
4153 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4154 {
4155         struct dentry *dentry = file_dentry(file);
4156         struct inode *inode = dentry->d_inode;
4157         struct ll_inode_info *lli = ll_i2info(inode);
4158         struct ptlrpc_request *req;
4159         ktime_t kstart = ktime_get();
4160         int rc, err;
4161
4162         ENTRY;
4163
4164         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), start %lld, end %lld,"
4165                "datasync %d\n",
4166                PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4167
4168         /* fsync's caller has already called _fdata{sync,write}, we want
4169          * that IO to finish before calling the osc and mdc sync methods */
4170         rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4171         inode_lock(inode);
4172
4173         /* catch async errors that were recorded back when async writeback
4174          * failed for pages in this mapping. */
4175         if (!S_ISDIR(inode->i_mode)) {
4176                 err = lli->lli_async_rc;
4177                 lli->lli_async_rc = 0;
4178                 if (rc == 0)
4179                         rc = err;
4180                 if (lli->lli_clob != NULL) {
4181                         err = lov_read_and_clear_async_rc(lli->lli_clob);
4182                         if (rc == 0)
4183                                 rc = err;
4184                 }
4185         }
4186
4187         err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4188         if (!rc)
4189                 rc = err;
4190         if (!err)
4191                 ptlrpc_req_finished(req);
4192
4193         if (S_ISREG(inode->i_mode)) {
4194                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
4195                 bool cached;
4196
4197                 /* Sync metadata on MDT first, and then sync the cached data
4198                  * on PCC.
4199                  */
4200                 err = pcc_fsync(file, start, end, datasync, &cached);
4201                 if (!cached)
4202                         err = cl_sync_file_range(inode, start, end,
4203                                                  CL_FSYNC_ALL, 0);
4204                 if (rc == 0 && err < 0)
4205                         rc = err;
4206                 if (rc < 0)
4207                         fd->fd_write_failed = true;
4208                 else
4209                         fd->fd_write_failed = false;
4210         }
4211
4212         inode_unlock(inode);
4213
4214         if (!rc)
4215                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4216                                    ktime_us_delta(ktime_get(), kstart));
4217         RETURN(rc);
4218 }
4219
4220 static int
4221 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4222 {
4223         struct inode *inode = file_inode(file);
4224         struct ll_sb_info *sbi = ll_i2sbi(inode);
4225         struct ldlm_enqueue_info einfo = {
4226                 .ei_type        = LDLM_FLOCK,
4227                 .ei_cb_cp       = ldlm_flock_completion_ast,
4228                 .ei_cbdata      = file_lock,
4229         };
4230         struct md_op_data *op_data;
4231         struct lustre_handle lockh = { 0 };
4232         union ldlm_policy_data flock = { { 0 } };
4233         int fl_type = file_lock->fl_type;
4234         ktime_t kstart = ktime_get();
4235         __u64 flags = 0;
4236         int rc;
4237         int rc2 = 0;
4238         ENTRY;
4239
4240         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4241                PFID(ll_inode2fid(inode)), file_lock);
4242
4243         if (file_lock->fl_flags & FL_FLOCK) {
4244                 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4245                 /* flocks are whole-file locks */
4246                 flock.l_flock.end = OFFSET_MAX;
4247                 /* For flocks owner is determined by the local file desctiptor*/
4248                 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4249         } else if (file_lock->fl_flags & FL_POSIX) {
4250                 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4251                 flock.l_flock.start = file_lock->fl_start;
4252                 flock.l_flock.end = file_lock->fl_end;
4253         } else {
4254                 RETURN(-EINVAL);
4255         }
4256         flock.l_flock.pid = file_lock->fl_pid;
4257
4258 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4259         /* Somewhat ugly workaround for svc lockd.
4260          * lockd installs custom fl_lmops->lm_compare_owner that checks
4261          * for the fl_owner to be the same (which it always is on local node
4262          * I guess between lockd processes) and then compares pid.
4263          * As such we assign pid to the owner field to make it all work,
4264          * conflict with normal locks is unlikely since pid space and
4265          * pointer space for current->files are not intersecting */
4266         if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4267                 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4268 #endif
4269
4270         switch (fl_type) {
4271         case F_RDLCK:
4272                 einfo.ei_mode = LCK_PR;
4273                 break;
4274         case F_UNLCK:
4275                 /* An unlock request may or may not have any relation to
4276                  * existing locks so we may not be able to pass a lock handle
4277                  * via a normal ldlm_lock_cancel() request. The request may even
4278                  * unlock a byte range in the middle of an existing lock. In
4279                  * order to process an unlock request we need all of the same
4280                  * information that is given with a normal read or write record
4281                  * lock request. To avoid creating another ldlm unlock (cancel)
4282                  * message we'll treat a LCK_NL flock request as an unlock. */
4283                 einfo.ei_mode = LCK_NL;
4284                 break;
4285         case F_WRLCK:
4286                 einfo.ei_mode = LCK_PW;
4287                 break;
4288         default:
4289                 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4290                 RETURN (-ENOTSUPP);
4291         }
4292
4293         switch (cmd) {
4294         case F_SETLKW:
4295 #ifdef F_SETLKW64
4296         case F_SETLKW64:
4297 #endif
4298                 flags = 0;
4299                 break;
4300         case F_SETLK:
4301 #ifdef F_SETLK64
4302         case F_SETLK64:
4303 #endif
4304                 flags = LDLM_FL_BLOCK_NOWAIT;
4305                 break;
4306         case F_GETLK:
4307 #ifdef F_GETLK64
4308         case F_GETLK64:
4309 #endif
4310                 flags = LDLM_FL_TEST_LOCK;
4311                 break;
4312         default:
4313                 CERROR("unknown fcntl lock command: %d\n", cmd);
4314                 RETURN (-EINVAL);
4315         }
4316
4317         /* Save the old mode so that if the mode in the lock changes we
4318          * can decrement the appropriate reader or writer refcount. */
4319         file_lock->fl_type = einfo.ei_mode;
4320
4321         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4322                                      LUSTRE_OPC_ANY, NULL);
4323         if (IS_ERR(op_data))
4324                 RETURN(PTR_ERR(op_data));
4325
4326         CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4327                "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4328                flock.l_flock.pid, flags, einfo.ei_mode,
4329                flock.l_flock.start, flock.l_flock.end);
4330
4331         rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4332                         flags);
4333
4334         /* Restore the file lock type if not TEST lock. */
4335         if (!(flags & LDLM_FL_TEST_LOCK))
4336                 file_lock->fl_type = fl_type;
4337
4338 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4339         if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4340             !(flags & LDLM_FL_TEST_LOCK))
4341                 rc2  = locks_lock_file_wait(file, file_lock);
4342 #else
4343         if ((file_lock->fl_flags & FL_FLOCK) &&
4344             (rc == 0 || file_lock->fl_type == F_UNLCK))
4345                 rc2  = flock_lock_file_wait(file, file_lock);
4346         if ((file_lock->fl_flags & FL_POSIX) &&
4347             (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4348             !(flags & LDLM_FL_TEST_LOCK))
4349                 rc2  = posix_lock_file_wait(file, file_lock);
4350 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4351
4352         if (rc2 && file_lock->fl_type != F_UNLCK) {
4353                 einfo.ei_mode = LCK_NL;
4354                 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4355                            &lockh, flags);
4356                 rc = rc2;
4357         }
4358
4359         ll_finish_md_op_data(op_data);
4360
4361         if (!rc)
4362                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4363                                    ktime_us_delta(ktime_get(), kstart));
4364         RETURN(rc);
4365 }
4366
4367 int ll_get_fid_by_name(struct inode *parent, const char *name,
4368                        int namelen, struct lu_fid *fid,
4369                        struct inode **inode)
4370 {
4371         struct md_op_data       *op_data = NULL;
4372         struct mdt_body         *body;
4373         struct ptlrpc_request   *req;
4374         int                     rc;
4375         ENTRY;
4376
4377         op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4378                                      LUSTRE_OPC_ANY, NULL);
4379         if (IS_ERR(op_data))
4380                 RETURN(PTR_ERR(op_data));
4381
4382         op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4383         rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4384         ll_finish_md_op_data(op_data);
4385         if (rc < 0)
4386                 RETURN(rc);
4387
4388         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4389         if (body == NULL)
4390                 GOTO(out_req, rc = -EFAULT);
4391         if (fid != NULL)
4392                 *fid = body->mbo_fid1;
4393
4394         if (inode != NULL)
4395                 rc = ll_prep_inode(inode, req, parent->i_sb, NULL);
4396 out_req:
4397         ptlrpc_req_finished(req);
4398         RETURN(rc);
4399 }
4400
4401 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4402                const char *name)
4403 {
4404         struct dentry *dchild = NULL;
4405         struct inode *child_inode = NULL;
4406         struct md_op_data *op_data;
4407         struct ptlrpc_request *request = NULL;
4408         struct obd_client_handle *och = NULL;
4409         struct qstr qstr;
4410         struct mdt_body *body;
4411         __u64 data_version = 0;
4412         size_t namelen = strlen(name);
4413         int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4414         int rc;
4415         ENTRY;
4416
4417         CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4418                PFID(ll_inode2fid(parent)), name,
4419                lum->lum_stripe_offset, lum->lum_stripe_count);
4420
4421         if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4422             lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4423                 lustre_swab_lmv_user_md(lum);
4424
4425         /* Get child FID first */
4426         qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4427         qstr.name = name;
4428         qstr.len = namelen;
4429         dchild = d_lookup(file_dentry(file), &qstr);
4430         if (dchild) {
4431                 if (dchild->d_inode)
4432                         child_inode = igrab(dchild->d_inode);
4433                 dput(dchild);
4434         }
4435
4436         if (!child_inode) {
4437                 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4438                                         &child_inode);
4439                 if (rc)
4440                         RETURN(rc);
4441         }
4442
4443         if (!child_inode)
4444                 RETURN(-ENOENT);
4445
4446         if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4447               OBD_CONNECT2_DIR_MIGRATE)) {
4448                 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4449                     ll_dir_striped(child_inode)) {
4450                         CERROR("%s: MDT doesn't support stripe directory "
4451                                "migration!\n", ll_i2sbi(parent)->ll_fsname);
4452                         GOTO(out_iput, rc = -EOPNOTSUPP);
4453                 }
4454         }
4455
4456         /*
4457          * lfs migrate command needs to be blocked on the client
4458          * by checking the migrate FID against the FID of the
4459          * filesystem root.
4460          */
4461         if (child_inode == parent->i_sb->s_root->d_inode)
4462                 GOTO(out_iput, rc = -EINVAL);
4463
4464         op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4465                                      child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4466         if (IS_ERR(op_data))
4467                 GOTO(out_iput, rc = PTR_ERR(op_data));
4468
4469         inode_lock(child_inode);
4470         op_data->op_fid3 = *ll_inode2fid(child_inode);
4471         if (!fid_is_sane(&op_data->op_fid3)) {
4472                 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4473                        ll_i2sbi(parent)->ll_fsname, name,
4474                        PFID(&op_data->op_fid3));
4475                 GOTO(out_unlock, rc = -EINVAL);
4476         }
4477
4478         op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4479         op_data->op_data = lum;
4480         op_data->op_data_size = lumlen;
4481
4482 again:
4483         if (S_ISREG(child_inode->i_mode)) {
4484                 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4485                 if (IS_ERR(och)) {
4486                         rc = PTR_ERR(och);
4487                         och = NULL;
4488                         GOTO(out_unlock, rc);
4489                 }
4490
4491                 rc = ll_data_version(child_inode, &data_version,
4492                                      LL_DV_WR_FLUSH);
4493                 if (rc != 0)
4494                         GOTO(out_close, rc);
4495
4496                 op_data->op_open_handle = och->och_open_handle;
4497                 op_data->op_data_version = data_version;
4498                 op_data->op_lease_handle = och->och_lease_handle;
4499                 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4500
4501                 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4502                 och->och_mod->mod_open_req->rq_replay = 0;
4503                 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4504         }
4505
4506         rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name, namelen,
4507                        name, namelen, &request);
4508         if (rc == 0) {
4509                 LASSERT(request != NULL);
4510                 ll_update_times(request, parent);
4511         }
4512
4513         if (rc == 0 || rc == -EAGAIN) {
4514                 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4515                 LASSERT(body != NULL);
4516
4517                 /* If the server does release layout lock, then we cleanup
4518                  * the client och here, otherwise release it in out_close: */
4519                 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4520                         obd_mod_put(och->och_mod);
4521                         md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4522                                                   och);
4523                         och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4524                         OBD_FREE_PTR(och);
4525                         och = NULL;
4526                 }
4527         }
4528
4529         if (request != NULL) {
4530                 ptlrpc_req_finished(request);
4531                 request = NULL;
4532         }
4533
4534         /* Try again if the lease has cancelled. */
4535         if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4536                 goto again;
4537
4538 out_close:
4539         if (och)
4540                 ll_lease_close(och, child_inode, NULL);
4541         if (!rc)
4542                 clear_nlink(child_inode);
4543 out_unlock:
4544         inode_unlock(child_inode);
4545         ll_finish_md_op_data(op_data);
4546 out_iput:
4547         iput(child_inode);
4548         RETURN(rc);
4549 }
4550
4551 static int
4552 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4553 {
4554         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
4555         ENTRY;
4556
4557         /*
4558          * In order to avoid flood of warning messages, only print one message
4559          * for one file. And the entire message rate on the client is limited
4560          * by CDEBUG_LIMIT too.
4561          */
4562         if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
4563                 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
4564                 CDEBUG_LIMIT(D_TTY | D_CONSOLE,
4565                              "flock disabled, mount with '-o [local]flock' to enable\r\n");
4566         }
4567         RETURN(-ENOSYS);
4568 }
4569
4570 /**
4571  * test if some locks matching bits and l_req_mode are acquired
4572  * - bits can be in different locks
4573  * - if found clear the common lock bits in *bits
4574  * - the bits not found, are kept in *bits
4575  * \param inode [IN]
4576  * \param bits [IN] searched lock bits [IN]
4577  * \param l_req_mode [IN] searched lock mode
4578  * \retval boolean, true iff all bits are found
4579  */
4580 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4581 {
4582         struct lustre_handle lockh;
4583         union ldlm_policy_data policy;
4584         enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4585                               (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
4586         struct lu_fid *fid;
4587         __u64 flags;
4588         int i;
4589         ENTRY;
4590
4591         if (!inode)
4592                RETURN(0);
4593
4594         fid = &ll_i2info(inode)->lli_fid;
4595         CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
4596                ldlm_lockname[mode]);
4597
4598         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
4599         for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
4600                 policy.l_inodebits.bits = *bits & (1 << i);
4601                 if (policy.l_inodebits.bits == 0)
4602                         continue;
4603
4604                 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
4605                                   &policy, mode, &lockh)) {
4606                         struct ldlm_lock *lock;
4607
4608                         lock = ldlm_handle2lock(&lockh);
4609                         if (lock) {
4610                                 *bits &=
4611                                       ~(lock->l_policy_data.l_inodebits.bits);
4612                                 LDLM_LOCK_PUT(lock);
4613                         } else {
4614                                 *bits &= ~policy.l_inodebits.bits;
4615                         }
4616                 }
4617         }
4618         RETURN(*bits == 0);
4619 }
4620
4621 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
4622                                struct lustre_handle *lockh, __u64 flags,
4623                                enum ldlm_mode mode)
4624 {
4625         union ldlm_policy_data policy = { .l_inodebits = { bits } };
4626         struct lu_fid *fid;
4627         enum ldlm_mode rc;
4628         ENTRY;
4629
4630         fid = &ll_i2info(inode)->lli_fid;
4631         CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
4632
4633         rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
4634                            fid, LDLM_IBITS, &policy, mode, lockh);
4635
4636         RETURN(rc);
4637 }
4638
4639 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
4640 {
4641         /* Already unlinked. Just update nlink and return success */
4642         if (rc == -ENOENT) {
4643                 clear_nlink(inode);
4644                 /* If it is striped directory, and there is bad stripe
4645                  * Let's revalidate the dentry again, instead of returning
4646                  * error */
4647                 if (ll_dir_striped(inode))
4648                         return 0;
4649
4650                 /* This path cannot be hit for regular files unless in
4651                  * case of obscure races, so no need to to validate
4652                  * size. */
4653                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
4654                         return 0;
4655         } else if (rc != 0) {
4656                 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
4657                              "%s: revalidate FID "DFID" error: rc = %d\n",
4658                              ll_i2sbi(inode)->ll_fsname,
4659                              PFID(ll_inode2fid(inode)), rc);
4660         }
4661
4662         return rc;
4663 }
4664
4665 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
4666 {
4667         struct inode *inode = dentry->d_inode;
4668         struct obd_export *exp = ll_i2mdexp(inode);
4669         struct lookup_intent oit = {
4670                 .it_op = op,
4671         };
4672         struct ptlrpc_request *req = NULL;
4673         struct md_op_data *op_data;
4674         int rc = 0;
4675         ENTRY;
4676
4677         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
4678                PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
4679
4680         /* Call getattr by fid, so do not provide name at all. */
4681         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
4682                                      LUSTRE_OPC_ANY, NULL);
4683         if (IS_ERR(op_data))
4684                 RETURN(PTR_ERR(op_data));
4685
4686         rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
4687         ll_finish_md_op_data(op_data);
4688         if (rc < 0) {
4689                 rc = ll_inode_revalidate_fini(inode, rc);
4690                 GOTO(out, rc);
4691         }
4692
4693         rc = ll_revalidate_it_finish(req, &oit, dentry);
4694         if (rc != 0) {
4695                 ll_intent_release(&oit);
4696                 GOTO(out, rc);
4697         }
4698
4699         /* Unlinked? Unhash dentry, so it is not picked up later by
4700          * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
4701          * here to preserve get_cwd functionality on 2.6.
4702          * Bug 10503 */
4703         if (!dentry->d_inode->i_nlink) {
4704                 spin_lock(&inode->i_lock);
4705                 d_lustre_invalidate(dentry, 0);
4706                 spin_unlock(&inode->i_lock);
4707         }
4708
4709         ll_lookup_finish_locks(&oit, dentry);
4710 out:
4711         ptlrpc_req_finished(req);
4712
4713         return rc;
4714 }
4715
4716 static int ll_merge_md_attr(struct inode *inode)
4717 {
4718         struct ll_inode_info *lli = ll_i2info(inode);
4719         struct cl_attr attr = { 0 };
4720         int rc;
4721
4722         LASSERT(lli->lli_lsm_md != NULL);
4723
4724         if (!lmv_dir_striped(lli->lli_lsm_md))
4725                 RETURN(0);
4726
4727         down_read(&lli->lli_lsm_sem);
4728         rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
4729                            &attr, ll_md_blocking_ast);
4730         up_read(&lli->lli_lsm_sem);
4731         if (rc != 0)
4732                 RETURN(rc);
4733
4734         set_nlink(inode, attr.cat_nlink);
4735         inode->i_blocks = attr.cat_blocks;
4736         i_size_write(inode, attr.cat_size);
4737
4738         ll_i2info(inode)->lli_atime = attr.cat_atime;
4739         ll_i2info(inode)->lli_mtime = attr.cat_mtime;
4740         ll_i2info(inode)->lli_ctime = attr.cat_ctime;
4741
4742         RETURN(0);
4743 }
4744
4745 int ll_getattr_dentry(struct dentry *de, struct kstat *stat)
4746 {
4747         struct inode *inode = de->d_inode;
4748         struct ll_sb_info *sbi = ll_i2sbi(inode);
4749         struct ll_inode_info *lli = ll_i2info(inode);
4750         ktime_t kstart = ktime_get();
4751         int rc;
4752
4753         rc = ll_inode_revalidate(de, IT_GETATTR);
4754         if (rc < 0)
4755                 RETURN(rc);
4756
4757         if (S_ISREG(inode->i_mode)) {
4758                 bool cached;
4759
4760                 rc = pcc_inode_getattr(inode, &cached);
4761                 if (cached && rc < 0)
4762                         RETURN(rc);
4763
4764                 /* In case of restore, the MDT has the right size and has
4765                  * already send it back without granting the layout lock,
4766                  * inode is up-to-date so glimpse is useless.
4767                  * Also to glimpse we need the layout, in case of a running
4768                  * restore the MDT holds the layout lock so the glimpse will
4769                  * block up to the end of restore (getattr will block)
4770                  */
4771                 if (!cached && !ll_file_test_flag(lli, LLIF_FILE_RESTORING)) {
4772                         rc = ll_glimpse_size(inode);
4773                         if (rc < 0)
4774                                 RETURN(rc);
4775                 }
4776         } else {
4777                 /* If object isn't regular a file then don't validate size. */
4778                 if (ll_dir_striped(inode)) {
4779                         rc = ll_merge_md_attr(inode);
4780                         if (rc < 0)
4781                                 RETURN(rc);
4782                 }
4783
4784                 inode->i_atime.tv_sec = lli->lli_atime;
4785                 inode->i_mtime.tv_sec = lli->lli_mtime;
4786                 inode->i_ctime.tv_sec = lli->lli_ctime;
4787         }
4788
4789         OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
4790
4791         if (ll_need_32bit_api(sbi)) {
4792                 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
4793                 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
4794                 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
4795         } else {
4796                 stat->ino = inode->i_ino;
4797                 stat->dev = inode->i_sb->s_dev;
4798                 stat->rdev = inode->i_rdev;
4799         }
4800
4801         stat->mode = inode->i_mode;
4802         stat->uid = inode->i_uid;
4803         stat->gid = inode->i_gid;
4804         stat->atime = inode->i_atime;
4805         stat->mtime = inode->i_mtime;
4806         stat->ctime = inode->i_ctime;
4807         stat->blksize = sbi->ll_stat_blksize ?: 1 << inode->i_blkbits;
4808
4809         stat->nlink = inode->i_nlink;
4810         stat->size = i_size_read(inode);
4811         stat->blocks = inode->i_blocks;
4812
4813         ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
4814                            ktime_us_delta(ktime_get(), kstart));
4815
4816         return 0;
4817 }
4818
4819 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
4820 int ll_getattr(const struct path *path, struct kstat *stat,
4821                u32 request_mask, unsigned int flags)
4822 {
4823         struct dentry *de = path->dentry;
4824 #else
4825 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
4826 {
4827 #endif
4828         return ll_getattr_dentry(de, stat);
4829 }
4830
4831 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4832                      __u64 start, __u64 len)
4833 {
4834         int             rc;
4835         size_t          num_bytes;
4836         struct fiemap   *fiemap;
4837         unsigned int    extent_count = fieinfo->fi_extents_max;
4838
4839         num_bytes = sizeof(*fiemap) + (extent_count *
4840                                        sizeof(struct fiemap_extent));
4841         OBD_ALLOC_LARGE(fiemap, num_bytes);
4842
4843         if (fiemap == NULL)
4844                 RETURN(-ENOMEM);
4845
4846         fiemap->fm_flags = fieinfo->fi_flags;
4847         fiemap->fm_extent_count = fieinfo->fi_extents_max;
4848         fiemap->fm_start = start;
4849         fiemap->fm_length = len;
4850         if (extent_count > 0 &&
4851             copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
4852                            sizeof(struct fiemap_extent)) != 0)
4853                 GOTO(out, rc = -EFAULT);
4854
4855         rc = ll_do_fiemap(inode, fiemap, num_bytes);
4856
4857         fieinfo->fi_flags = fiemap->fm_flags;
4858         fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
4859         if (extent_count > 0 &&
4860             copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
4861                          fiemap->fm_mapped_extents *
4862                          sizeof(struct fiemap_extent)) != 0)
4863                 GOTO(out, rc = -EFAULT);
4864 out:
4865         OBD_FREE_LARGE(fiemap, num_bytes);
4866         return rc;
4867 }
4868
4869 struct posix_acl *ll_get_acl(struct inode *inode, int type)
4870 {
4871         struct ll_inode_info *lli = ll_i2info(inode);
4872         struct posix_acl *acl = NULL;
4873         ENTRY;
4874
4875         spin_lock(&lli->lli_lock);
4876         /* VFS' acl_permission_check->check_acl will release the refcount */
4877         acl = posix_acl_dup(lli->lli_posix_acl);
4878         spin_unlock(&lli->lli_lock);
4879
4880         RETURN(acl);
4881 }
4882
4883 #ifdef HAVE_IOP_SET_ACL
4884 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
4885 int ll_set_acl(struct inode *inode, struct posix_acl *acl, int type)
4886 {
4887         struct ll_sb_info *sbi = ll_i2sbi(inode);
4888         struct ptlrpc_request *req = NULL;
4889         const char *name = NULL;
4890         char *value = NULL;
4891         size_t value_size = 0;
4892         int rc = 0;
4893         ENTRY;
4894
4895         switch (type) {
4896         case ACL_TYPE_ACCESS:
4897                 name = XATTR_NAME_POSIX_ACL_ACCESS;
4898                 if (acl)
4899                         rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
4900                 break;
4901
4902         case ACL_TYPE_DEFAULT:
4903                 name = XATTR_NAME_POSIX_ACL_DEFAULT;
4904                 if (!S_ISDIR(inode->i_mode))
4905                         rc = acl ? -EACCES : 0;
4906                 break;
4907
4908         default:
4909                 rc = -EINVAL;
4910                 break;
4911         }
4912         if (rc)
4913                 return rc;
4914
4915         if (acl) {
4916                 value_size = posix_acl_xattr_size(acl->a_count);
4917                 value = kmalloc(value_size, GFP_NOFS);
4918                 if (value == NULL)
4919                         GOTO(out, rc = -ENOMEM);
4920
4921                 rc = posix_acl_to_xattr(&init_user_ns, acl, value, value_size);
4922                 if (rc < 0)
4923                         GOTO(out_value, rc);
4924         }
4925
4926         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
4927                          value ? OBD_MD_FLXATTR : OBD_MD_FLXATTRRM,
4928                          name, value, value_size, 0, 0, &req);
4929
4930         ptlrpc_req_finished(req);
4931 out_value:
4932         kfree(value);
4933 out:
4934         if (rc)
4935                 forget_cached_acl(inode, type);
4936         else
4937                 set_cached_acl(inode, type, acl);
4938         RETURN(rc);
4939 }
4940 #endif /* CONFIG_LUSTRE_FS_POSIX_ACL */
4941 #endif /* HAVE_IOP_SET_ACL */
4942
4943 int ll_inode_permission(struct inode *inode, int mask)
4944 {
4945         int rc = 0;
4946         struct ll_sb_info *sbi;
4947         struct root_squash_info *squash;
4948         struct cred *cred = NULL;
4949         const struct cred *old_cred = NULL;
4950         cfs_cap_t cap;
4951         bool squash_id = false;
4952         ktime_t kstart = ktime_get();
4953         ENTRY;
4954
4955         if (mask & MAY_NOT_BLOCK)
4956                 return -ECHILD;
4957
4958        /* as root inode are NOT getting validated in lookup operation,
4959         * need to do it before permission check. */
4960
4961         if (inode == inode->i_sb->s_root->d_inode) {
4962                 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
4963                 if (rc)
4964                         RETURN(rc);
4965         }
4966
4967         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
4968                PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
4969
4970         /* squash fsuid/fsgid if needed */
4971         sbi = ll_i2sbi(inode);
4972         squash = &sbi->ll_squash;
4973         if (unlikely(squash->rsi_uid != 0 &&
4974                      uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
4975                      !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
4976                         squash_id = true;
4977         }
4978         if (squash_id) {
4979                 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
4980                        __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
4981                        squash->rsi_uid, squash->rsi_gid);
4982
4983                 /* update current process's credentials
4984                  * and FS capability */
4985                 cred = prepare_creds();
4986                 if (cred == NULL)
4987                         RETURN(-ENOMEM);
4988
4989                 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
4990                 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
4991                 for (cap = 0; cap < sizeof(cfs_cap_t) * 8; cap++) {
4992                         if ((1 << cap) & CFS_CAP_FS_MASK)
4993                                 cap_lower(cred->cap_effective, cap);
4994                 }
4995                 old_cred = override_creds(cred);
4996         }
4997
4998         rc = generic_permission(inode, mask);
4999         /* restore current process's credentials and FS capability */
5000         if (squash_id) {
5001                 revert_creds(old_cred);
5002                 put_cred(cred);
5003         }
5004
5005         if (!rc)
5006                 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5007                                    ktime_us_delta(ktime_get(), kstart));
5008
5009         RETURN(rc);
5010 }
5011
5012 /* -o localflock - only provides locally consistent flock locks */
5013 struct file_operations ll_file_operations = {
5014 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5015 # ifdef HAVE_SYNC_READ_WRITE
5016         .read           = new_sync_read,
5017         .write          = new_sync_write,
5018 # endif
5019         .read_iter      = ll_file_read_iter,
5020         .write_iter     = ll_file_write_iter,
5021 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5022         .read           = ll_file_read,
5023         .aio_read       = ll_file_aio_read,
5024         .write          = ll_file_write,
5025         .aio_write      = ll_file_aio_write,
5026 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5027         .unlocked_ioctl = ll_file_ioctl,
5028         .open           = ll_file_open,
5029         .release        = ll_file_release,
5030         .mmap           = ll_file_mmap,
5031         .llseek         = ll_file_seek,
5032         .splice_read    = ll_file_splice_read,
5033         .fsync          = ll_fsync,
5034         .flush          = ll_flush
5035 };
5036
5037 struct file_operations ll_file_operations_flock = {
5038 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5039 # ifdef HAVE_SYNC_READ_WRITE
5040         .read           = new_sync_read,
5041         .write          = new_sync_write,
5042 # endif /* HAVE_SYNC_READ_WRITE */
5043         .read_iter      = ll_file_read_iter,
5044         .write_iter     = ll_file_write_iter,
5045 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5046         .read           = ll_file_read,
5047         .aio_read       = ll_file_aio_read,
5048         .write          = ll_file_write,
5049         .aio_write      = ll_file_aio_write,
5050 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5051         .unlocked_ioctl = ll_file_ioctl,
5052         .open           = ll_file_open,
5053         .release        = ll_file_release,
5054         .mmap           = ll_file_mmap,
5055         .llseek         = ll_file_seek,
5056         .splice_read    = ll_file_splice_read,
5057         .fsync          = ll_fsync,
5058         .flush          = ll_flush,
5059         .flock          = ll_file_flock,
5060         .lock           = ll_file_flock
5061 };
5062
5063 /* These are for -o noflock - to return ENOSYS on flock calls */
5064 struct file_operations ll_file_operations_noflock = {
5065 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5066 # ifdef HAVE_SYNC_READ_WRITE
5067         .read           = new_sync_read,
5068         .write          = new_sync_write,
5069 # endif /* HAVE_SYNC_READ_WRITE */
5070         .read_iter      = ll_file_read_iter,
5071         .write_iter     = ll_file_write_iter,
5072 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5073         .read           = ll_file_read,
5074         .aio_read       = ll_file_aio_read,
5075         .write          = ll_file_write,
5076         .aio_write      = ll_file_aio_write,
5077 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5078         .unlocked_ioctl = ll_file_ioctl,
5079         .open           = ll_file_open,
5080         .release        = ll_file_release,
5081         .mmap           = ll_file_mmap,
5082         .llseek         = ll_file_seek,
5083         .splice_read    = ll_file_splice_read,
5084         .fsync          = ll_fsync,
5085         .flush          = ll_flush,
5086         .flock          = ll_file_noflock,
5087         .lock           = ll_file_noflock
5088 };
5089
5090 struct inode_operations ll_file_inode_operations = {
5091         .setattr        = ll_setattr,
5092         .getattr        = ll_getattr,
5093         .permission     = ll_inode_permission,
5094 #ifdef HAVE_IOP_XATTR
5095         .setxattr       = ll_setxattr,
5096         .getxattr       = ll_getxattr,
5097         .removexattr    = ll_removexattr,
5098 #endif
5099         .listxattr      = ll_listxattr,
5100         .fiemap         = ll_fiemap,
5101 #ifdef HAVE_IOP_GET_ACL
5102         .get_acl        = ll_get_acl,
5103 #endif
5104 #ifdef HAVE_IOP_SET_ACL
5105         .set_acl        = ll_set_acl,
5106 #endif
5107 };
5108
5109 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5110 {
5111         struct ll_inode_info *lli = ll_i2info(inode);
5112         struct cl_object *obj = lli->lli_clob;
5113         struct lu_env *env;
5114         int rc;
5115         __u16 refcheck;
5116         ENTRY;
5117
5118         if (obj == NULL)
5119                 RETURN(0);
5120
5121         env = cl_env_get(&refcheck);
5122         if (IS_ERR(env))
5123                 RETURN(PTR_ERR(env));
5124
5125         rc = cl_conf_set(env, lli->lli_clob, conf);
5126         if (rc < 0)
5127                 GOTO(out, rc);
5128
5129         if (conf->coc_opc == OBJECT_CONF_SET) {
5130                 struct ldlm_lock *lock = conf->coc_lock;
5131                 struct cl_layout cl = {
5132                         .cl_layout_gen = 0,
5133                 };
5134
5135                 LASSERT(lock != NULL);
5136                 LASSERT(ldlm_has_layout(lock));
5137
5138                 /* it can only be allowed to match after layout is
5139                  * applied to inode otherwise false layout would be
5140                  * seen. Applying layout shoud happen before dropping
5141                  * the intent lock. */
5142                 ldlm_lock_allow_match(lock);
5143
5144                 rc = cl_object_layout_get(env, obj, &cl);
5145                 if (rc < 0)
5146                         GOTO(out, rc);
5147
5148                 CDEBUG(D_VFSTRACE,
5149                        DFID": layout version change: %u -> %u\n",
5150                        PFID(&lli->lli_fid), ll_layout_version_get(lli),
5151                        cl.cl_layout_gen);
5152                 ll_layout_version_set(lli, cl.cl_layout_gen);
5153         }
5154
5155 out:
5156         cl_env_put(env, &refcheck);
5157
5158         RETURN(rc);
5159 }
5160
5161 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5162 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5163
5164 {
5165         struct ll_sb_info *sbi = ll_i2sbi(inode);
5166         struct ptlrpc_request *req;
5167         void *lvbdata;
5168         void *lmm;
5169         int lmmsize;
5170         int rc;
5171         ENTRY;
5172
5173         CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5174                PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5175                lock->l_lvb_data, lock->l_lvb_len);
5176
5177         if (lock->l_lvb_data != NULL)
5178                 RETURN(0);
5179
5180         /* if layout lock was granted right away, the layout is returned
5181          * within DLM_LVB of dlm reply; otherwise if the lock was ever
5182          * blocked and then granted via completion ast, we have to fetch
5183          * layout here. Please note that we can't use the LVB buffer in
5184          * completion AST because it doesn't have a large enough buffer */
5185         rc = ll_get_default_mdsize(sbi, &lmmsize);
5186         if (rc < 0)
5187                 RETURN(rc);
5188
5189         rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5190                          XATTR_NAME_LOV, lmmsize, &req);
5191         if (rc < 0) {
5192                 if (rc == -ENODATA)
5193                         GOTO(out, rc = 0); /* empty layout */
5194                 else
5195                         RETURN(rc);
5196         }
5197
5198         lmmsize = rc;
5199         rc = 0;
5200         if (lmmsize == 0) /* empty layout */
5201                 GOTO(out, rc = 0);
5202
5203         lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5204         if (lmm == NULL)
5205                 GOTO(out, rc = -EFAULT);
5206
5207         OBD_ALLOC_LARGE(lvbdata, lmmsize);
5208         if (lvbdata == NULL)
5209                 GOTO(out, rc = -ENOMEM);
5210
5211         memcpy(lvbdata, lmm, lmmsize);
5212         lock_res_and_lock(lock);
5213         if (unlikely(lock->l_lvb_data == NULL)) {
5214                 lock->l_lvb_type = LVB_T_LAYOUT;
5215                 lock->l_lvb_data = lvbdata;
5216                 lock->l_lvb_len = lmmsize;
5217                 lvbdata = NULL;
5218         }
5219         unlock_res_and_lock(lock);
5220
5221         if (lvbdata)
5222                 OBD_FREE_LARGE(lvbdata, lmmsize);
5223
5224         EXIT;
5225
5226 out:
5227         ptlrpc_req_finished(req);
5228         return rc;
5229 }
5230
5231 /**
5232  * Apply the layout to the inode. Layout lock is held and will be released
5233  * in this function.
5234  */
5235 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5236                               struct inode *inode)
5237 {
5238         struct ll_inode_info *lli = ll_i2info(inode);
5239         struct ll_sb_info    *sbi = ll_i2sbi(inode);
5240         struct ldlm_lock *lock;
5241         struct cl_object_conf conf;
5242         int rc = 0;
5243         bool lvb_ready;
5244         bool wait_layout = false;
5245         ENTRY;
5246
5247         LASSERT(lustre_handle_is_used(lockh));
5248
5249         lock = ldlm_handle2lock(lockh);
5250         LASSERT(lock != NULL);
5251         LASSERT(ldlm_has_layout(lock));
5252
5253         LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5254                    PFID(&lli->lli_fid), inode);
5255
5256         /* in case this is a caching lock and reinstate with new inode */
5257         md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5258
5259         lock_res_and_lock(lock);
5260         lvb_ready = ldlm_is_lvb_ready(lock);
5261         unlock_res_and_lock(lock);
5262
5263         /* checking lvb_ready is racy but this is okay. The worst case is
5264          * that multi processes may configure the file on the same time. */
5265         if (lvb_ready)
5266                 GOTO(out, rc = 0);
5267
5268         rc = ll_layout_fetch(inode, lock);
5269         if (rc < 0)
5270                 GOTO(out, rc);
5271
5272         /* for layout lock, lmm is stored in lock's lvb.
5273          * lvb_data is immutable if the lock is held so it's safe to access it
5274          * without res lock.
5275          *
5276          * set layout to file. Unlikely this will fail as old layout was
5277          * surely eliminated */
5278         memset(&conf, 0, sizeof conf);
5279         conf.coc_opc = OBJECT_CONF_SET;
5280         conf.coc_inode = inode;
5281         conf.coc_lock = lock;
5282         conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5283         conf.u.coc_layout.lb_len = lock->l_lvb_len;
5284         rc = ll_layout_conf(inode, &conf);
5285
5286         /* refresh layout failed, need to wait */
5287         wait_layout = rc == -EBUSY;
5288         EXIT;
5289 out:
5290         LDLM_LOCK_PUT(lock);
5291         ldlm_lock_decref(lockh, mode);
5292
5293         /* wait for IO to complete if it's still being used. */
5294         if (wait_layout) {
5295                 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5296                        sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5297
5298                 memset(&conf, 0, sizeof conf);
5299                 conf.coc_opc = OBJECT_CONF_WAIT;
5300                 conf.coc_inode = inode;
5301                 rc = ll_layout_conf(inode, &conf);
5302                 if (rc == 0)
5303                         rc = -EAGAIN;
5304
5305                 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5306                        sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5307         }
5308         RETURN(rc);
5309 }
5310
5311 /**
5312  * Issue layout intent RPC to MDS.
5313  * \param inode [in]    file inode
5314  * \param intent [in]   layout intent
5315  *
5316  * \retval 0    on success
5317  * \retval < 0  error code
5318  */
5319 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5320 {
5321         struct ll_inode_info  *lli = ll_i2info(inode);
5322         struct ll_sb_info     *sbi = ll_i2sbi(inode);
5323         struct md_op_data     *op_data;
5324         struct lookup_intent it;
5325         struct ptlrpc_request *req;
5326         int rc;
5327         ENTRY;
5328
5329         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5330                                      0, 0, LUSTRE_OPC_ANY, NULL);
5331         if (IS_ERR(op_data))
5332                 RETURN(PTR_ERR(op_data));
5333
5334         op_data->op_data = intent;
5335         op_data->op_data_size = sizeof(*intent);
5336
5337         memset(&it, 0, sizeof(it));
5338         it.it_op = IT_LAYOUT;
5339         if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5340             intent->li_opc == LAYOUT_INTENT_TRUNC)
5341                 it.it_flags = FMODE_WRITE;
5342
5343         LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5344                           sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5345
5346         rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5347                             &ll_md_blocking_ast, 0);
5348         if (it.it_request != NULL)
5349                 ptlrpc_req_finished(it.it_request);
5350         it.it_request = NULL;
5351
5352         ll_finish_md_op_data(op_data);
5353
5354         /* set lock data in case this is a new lock */
5355         if (!rc)
5356                 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5357
5358         ll_intent_drop_lock(&it);
5359
5360         RETURN(rc);
5361 }
5362
5363 /**
5364  * This function checks if there exists a LAYOUT lock on the client side,
5365  * or enqueues it if it doesn't have one in cache.
5366  *
5367  * This function will not hold layout lock so it may be revoked any time after
5368  * this function returns. Any operations depend on layout should be redone
5369  * in that case.
5370  *
5371  * This function should be called before lov_io_init() to get an uptodate
5372  * layout version, the caller should save the version number and after IO
5373  * is finished, this function should be called again to verify that layout
5374  * is not changed during IO time.
5375  */
5376 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5377 {
5378         struct ll_inode_info    *lli = ll_i2info(inode);
5379         struct ll_sb_info       *sbi = ll_i2sbi(inode);
5380         struct lustre_handle lockh;
5381         struct layout_intent intent = {
5382                 .li_opc = LAYOUT_INTENT_ACCESS,
5383         };
5384         enum ldlm_mode mode;
5385         int rc;
5386         ENTRY;
5387
5388         *gen = ll_layout_version_get(lli);
5389         if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
5390                 RETURN(0);
5391
5392         /* sanity checks */
5393         LASSERT(fid_is_sane(ll_inode2fid(inode)));
5394         LASSERT(S_ISREG(inode->i_mode));
5395
5396         /* take layout lock mutex to enqueue layout lock exclusively. */
5397         mutex_lock(&lli->lli_layout_mutex);
5398
5399         while (1) {
5400                 /* mostly layout lock is caching on the local side, so try to
5401                  * match it before grabbing layout lock mutex. */
5402                 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
5403                                        LCK_CR | LCK_CW | LCK_PR |
5404                                        LCK_PW | LCK_EX);
5405                 if (mode != 0) { /* hit cached lock */
5406                         rc = ll_layout_lock_set(&lockh, mode, inode);
5407                         if (rc == -EAGAIN)
5408                                 continue;
5409                         break;
5410                 }
5411
5412                 rc = ll_layout_intent(inode, &intent);
5413                 if (rc != 0)
5414                         break;
5415         }
5416
5417         if (rc == 0)
5418                 *gen = ll_layout_version_get(lli);
5419         mutex_unlock(&lli->lli_layout_mutex);
5420
5421         RETURN(rc);
5422 }
5423
5424 /**
5425  * Issue layout intent RPC indicating where in a file an IO is about to write.
5426  *
5427  * \param[in] inode     file inode.
5428  * \param[in] ext       write range with start offset of fille in bytes where
5429  *                      an IO is about to write, and exclusive end offset in
5430  *                      bytes.
5431  *
5432  * \retval 0    on success
5433  * \retval < 0  error code
5434  */
5435 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
5436                            struct lu_extent *ext)
5437 {
5438         struct layout_intent intent = {
5439                 .li_opc = opc,
5440                 .li_extent.e_start = ext->e_start,
5441                 .li_extent.e_end = ext->e_end,
5442         };
5443         int rc;
5444         ENTRY;
5445
5446         rc = ll_layout_intent(inode, &intent);
5447
5448         RETURN(rc);
5449 }
5450
5451 /**
5452  *  This function send a restore request to the MDT
5453  */
5454 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
5455 {
5456         struct hsm_user_request *hur;
5457         int                      len, rc;
5458         ENTRY;
5459
5460         len = sizeof(struct hsm_user_request) +
5461               sizeof(struct hsm_user_item);
5462         OBD_ALLOC(hur, len);
5463         if (hur == NULL)
5464                 RETURN(-ENOMEM);
5465
5466         hur->hur_request.hr_action = HUA_RESTORE;
5467         hur->hur_request.hr_archive_id = 0;
5468         hur->hur_request.hr_flags = 0;
5469         memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
5470                sizeof(hur->hur_user_item[0].hui_fid));
5471         hur->hur_user_item[0].hui_extent.offset = offset;
5472         hur->hur_user_item[0].hui_extent.length = length;
5473         hur->hur_request.hr_itemcount = 1;
5474         rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,
5475                            len, hur, NULL);
5476         OBD_FREE(hur, len);
5477         RETURN(rc);
5478 }