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