Whamcloud - gitweb
LU-12262 llite: harden ll_sbi ll_flags
[fs/lustre-release.git] / lustre / llite / llite_lib.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) 2003, 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/llite_lib.c
32  *
33  * Lustre Light Super operations
34  */
35
36 #define DEBUG_SUBSYSTEM S_LLITE
37
38 #include <linux/cpu.h>
39 #include <linux/module.h>
40 #include <linux/random.h>
41 #include <linux/statfs.h>
42 #include <linux/time.h>
43 #include <linux/types.h>
44 #include <libcfs/linux/linux-uuid.h>
45 #include <linux/version.h>
46 #include <linux/mm.h>
47 #include <linux/user_namespace.h>
48 #include <linux/delay.h>
49 #include <linux/uidgid.h>
50 #include <linux/fs_struct.h>
51
52 #ifndef HAVE_CPUS_READ_LOCK
53 #include <libcfs/linux/linux-cpu.h>
54 #endif
55 #include <libcfs/linux/linux-misc.h>
56 #include <uapi/linux/lustre/lustre_ioctl.h>
57 #ifdef HAVE_UAPI_LINUX_MOUNT_H
58 #include <uapi/linux/mount.h>
59 #endif
60
61 #include <lustre_ha.h>
62 #include <lustre_dlm.h>
63 #include <lprocfs_status.h>
64 #include <lustre_disk.h>
65 #include <uapi/linux/lustre/lustre_param.h>
66 #include <lustre_log.h>
67 #include <cl_object.h>
68 #include <obd_cksum.h>
69 #include "llite_internal.h"
70
71 struct kmem_cache *ll_file_data_slab;
72
73 #ifndef log2
74 #define log2(n) ffz(~(n))
75 #endif
76
77 /**
78  * If there is only one number of core visible to Lustre,
79  * async readahead will be disabled, to avoid massive over
80  * subscription, we use 1/2 of active cores as default max
81  * async readahead requests.
82  */
83 static inline unsigned int ll_get_ra_async_max_active(void)
84 {
85         return cfs_cpt_weight(cfs_cpt_tab, CFS_CPT_ANY) >> 1;
86 }
87
88 static struct ll_sb_info *ll_init_sbi(void)
89 {
90         struct ll_sb_info *sbi = NULL;
91         unsigned long pages;
92         unsigned long lru_page_max;
93         struct sysinfo si;
94         int rc;
95         int i;
96
97         ENTRY;
98
99         OBD_ALLOC_PTR(sbi);
100         if (sbi == NULL)
101                 RETURN(ERR_PTR(-ENOMEM));
102
103         rc = pcc_super_init(&sbi->ll_pcc_super);
104         if (rc < 0)
105                 GOTO(out_sbi, rc);
106
107         spin_lock_init(&sbi->ll_lock);
108         mutex_init(&sbi->ll_lco.lco_lock);
109         spin_lock_init(&sbi->ll_pp_extent_lock);
110         spin_lock_init(&sbi->ll_process_lock);
111         sbi->ll_rw_stats_on = 0;
112         sbi->ll_statfs_max_age = OBD_STATFS_CACHE_SECONDS;
113
114         si_meminfo(&si);
115         pages = si.totalram - si.totalhigh;
116         lru_page_max = pages / 2;
117
118         sbi->ll_ra_info.ra_async_max_active = ll_get_ra_async_max_active();
119         sbi->ll_ra_info.ll_readahead_wq =
120                 cfs_cpt_bind_workqueue("ll-readahead-wq", cfs_cpt_tab,
121                                        0, CFS_CPT_ANY,
122                                        sbi->ll_ra_info.ra_async_max_active);
123         if (IS_ERR(sbi->ll_ra_info.ll_readahead_wq))
124                 GOTO(out_pcc, rc = PTR_ERR(sbi->ll_ra_info.ll_readahead_wq));
125
126         /* initialize ll_cache data */
127         sbi->ll_cache = cl_cache_init(lru_page_max);
128         if (sbi->ll_cache == NULL)
129                 GOTO(out_destroy_ra, rc = -ENOMEM);
130
131         /* initialize foreign symlink prefix path */
132         OBD_ALLOC(sbi->ll_foreign_symlink_prefix, sizeof("/mnt/"));
133         if (sbi->ll_foreign_symlink_prefix == NULL)
134                 GOTO(out_destroy_ra, rc = -ENOMEM);
135         memcpy(sbi->ll_foreign_symlink_prefix, "/mnt/", sizeof("/mnt/"));
136         sbi->ll_foreign_symlink_prefix_size = sizeof("/mnt/");
137
138         /* initialize foreign symlink upcall path, none by default */
139         OBD_ALLOC(sbi->ll_foreign_symlink_upcall, sizeof("none"));
140         if (sbi->ll_foreign_symlink_upcall == NULL)
141                 GOTO(out_destroy_ra, rc = -ENOMEM);
142         memcpy(sbi->ll_foreign_symlink_upcall, "none", sizeof("none"));
143         sbi->ll_foreign_symlink_upcall_items = NULL;
144         sbi->ll_foreign_symlink_upcall_nb_items = 0;
145         init_rwsem(&sbi->ll_foreign_symlink_sem);
146         /* foreign symlink support (LL_SBI_FOREIGN_SYMLINK in ll_flags)
147          * not enabled by default
148          */
149
150         sbi->ll_ra_info.ra_max_pages =
151                 min(pages / 32, SBI_DEFAULT_READ_AHEAD_MAX);
152         sbi->ll_ra_info.ra_max_pages_per_file =
153                 min(sbi->ll_ra_info.ra_max_pages / 4,
154                     SBI_DEFAULT_READ_AHEAD_PER_FILE_MAX);
155         sbi->ll_ra_info.ra_async_pages_per_file_threshold =
156                                 sbi->ll_ra_info.ra_max_pages_per_file;
157         sbi->ll_ra_info.ra_range_pages = SBI_DEFAULT_RA_RANGE_PAGES;
158         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = -1;
159         atomic_set(&sbi->ll_ra_info.ra_async_inflight, 0);
160
161         set_bit(LL_SBI_VERBOSE, sbi->ll_flags);
162 #ifdef ENABLE_CHECKSUM
163         set_bit(LL_SBI_CHECKSUM, sbi->ll_flags);
164 #endif
165 #ifdef ENABLE_FLOCK
166         set_bit(LL_SBI_FLOCK, sbi->ll_flags);
167 #endif
168
169 #ifdef HAVE_LRU_RESIZE_SUPPORT
170         set_bit(LL_SBI_LRU_RESIZE, sbi->ll_flags);
171 #endif
172         set_bit(LL_SBI_LAZYSTATFS, sbi->ll_flags);
173
174         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
175                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
176                                pp_r_hist.oh_lock);
177                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
178                                pp_w_hist.oh_lock);
179         }
180
181         /* metadata statahead is enabled by default */
182         sbi->ll_sa_running_max = LL_SA_RUNNING_DEF;
183         sbi->ll_sa_max = LL_SA_RPC_DEF;
184         atomic_set(&sbi->ll_sa_total, 0);
185         atomic_set(&sbi->ll_sa_wrong, 0);
186         atomic_set(&sbi->ll_sa_running, 0);
187         atomic_set(&sbi->ll_agl_total, 0);
188         set_bit(LL_SBI_AGL_ENABLED, sbi->ll_flags);
189         set_bit(LL_SBI_FAST_READ, sbi->ll_flags);
190         set_bit(LL_SBI_TINY_WRITE, sbi->ll_flags);
191         set_bit(LL_SBI_PARALLEL_DIO, sbi->ll_flags);
192         ll_sbi_set_encrypt(sbi, true);
193
194         /* root squash */
195         sbi->ll_squash.rsi_uid = 0;
196         sbi->ll_squash.rsi_gid = 0;
197         INIT_LIST_HEAD(&sbi->ll_squash.rsi_nosquash_nids);
198         spin_lock_init(&sbi->ll_squash.rsi_lock);
199
200         /* Per-filesystem file heat */
201         sbi->ll_heat_decay_weight = SBI_DEFAULT_HEAT_DECAY_WEIGHT;
202         sbi->ll_heat_period_second = SBI_DEFAULT_HEAT_PERIOD_SECOND;
203
204         /* Per-fs open heat level before requesting open lock */
205         sbi->ll_oc_thrsh_count = SBI_DEFAULT_OPENCACHE_THRESHOLD_COUNT;
206         sbi->ll_oc_max_ms = SBI_DEFAULT_OPENCACHE_THRESHOLD_MAX_MS;
207         sbi->ll_oc_thrsh_ms = SBI_DEFAULT_OPENCACHE_THRESHOLD_MS;
208         RETURN(sbi);
209 out_destroy_ra:
210         if (sbi->ll_foreign_symlink_prefix)
211                 OBD_FREE(sbi->ll_foreign_symlink_prefix, sizeof("/mnt/"));
212         if (sbi->ll_cache) {
213                 cl_cache_decref(sbi->ll_cache);
214                 sbi->ll_cache = NULL;
215         }
216         destroy_workqueue(sbi->ll_ra_info.ll_readahead_wq);
217 out_pcc:
218         pcc_super_fini(&sbi->ll_pcc_super);
219 out_sbi:
220         OBD_FREE_PTR(sbi);
221         RETURN(ERR_PTR(rc));
222 }
223
224 static void ll_free_sbi(struct super_block *sb)
225 {
226         struct ll_sb_info *sbi = ll_s2sbi(sb);
227         ENTRY;
228
229         if (sbi != NULL) {
230                 if (!list_empty(&sbi->ll_squash.rsi_nosquash_nids))
231                         cfs_free_nidlist(&sbi->ll_squash.rsi_nosquash_nids);
232                 if (sbi->ll_ra_info.ll_readahead_wq)
233                         destroy_workqueue(sbi->ll_ra_info.ll_readahead_wq);
234                 if (sbi->ll_cache != NULL) {
235                         cl_cache_decref(sbi->ll_cache);
236                         sbi->ll_cache = NULL;
237                 }
238                 if (sbi->ll_foreign_symlink_prefix) {
239                         OBD_FREE(sbi->ll_foreign_symlink_prefix,
240                                  sbi->ll_foreign_symlink_prefix_size);
241                         sbi->ll_foreign_symlink_prefix = NULL;
242                 }
243                 if (sbi->ll_foreign_symlink_upcall) {
244                         OBD_FREE(sbi->ll_foreign_symlink_upcall,
245                                  strlen(sbi->ll_foreign_symlink_upcall) +
246                                        1);
247                         sbi->ll_foreign_symlink_upcall = NULL;
248                 }
249                 if (sbi->ll_foreign_symlink_upcall_items) {
250                         int i;
251                         int nb_items = sbi->ll_foreign_symlink_upcall_nb_items;
252                         struct ll_foreign_symlink_upcall_item *items =
253                                 sbi->ll_foreign_symlink_upcall_items;
254
255                         for (i = 0 ; i < nb_items; i++)
256                                 if (items[i].type == STRING_TYPE)
257                                         OBD_FREE(items[i].string,
258                                                        items[i].size);
259
260                         OBD_FREE_LARGE(items, nb_items *
261                                 sizeof(struct ll_foreign_symlink_upcall_item));
262                         sbi->ll_foreign_symlink_upcall_items = NULL;
263                 }
264                 pcc_super_fini(&sbi->ll_pcc_super);
265                 OBD_FREE(sbi, sizeof(*sbi));
266         }
267         EXIT;
268 }
269
270 static int client_common_fill_super(struct super_block *sb, char *md, char *dt)
271 {
272         struct inode *root = NULL;
273         struct ll_sb_info *sbi = ll_s2sbi(sb);
274         struct obd_statfs *osfs = NULL;
275         struct ptlrpc_request *request = NULL;
276         struct obd_connect_data *data = NULL;
277         struct obd_uuid *uuid;
278         struct md_op_data *op_data;
279         struct lustre_md lmd;
280         u64 valid;
281         int size, err, checksum;
282         bool api32;
283
284         ENTRY;
285         sbi->ll_md_obd = class_name2obd(md);
286         if (!sbi->ll_md_obd) {
287                 CERROR("MD %s: not setup or attached\n", md);
288                 RETURN(-EINVAL);
289         }
290
291         OBD_ALLOC_PTR(data);
292         if (data == NULL)
293                 RETURN(-ENOMEM);
294
295         OBD_ALLOC_PTR(osfs);
296         if (osfs == NULL) {
297                 OBD_FREE_PTR(data);
298                 RETURN(-ENOMEM);
299         }
300
301         /* pass client page size via ocd_grant_blkbits, the server should report
302          * back its backend blocksize for grant calculation purpose */
303         data->ocd_grant_blkbits = PAGE_SHIFT;
304
305         /* indicate MDT features supported by this client */
306         data->ocd_connect_flags = OBD_CONNECT_IBITS    | OBD_CONNECT_NODEVOH  |
307                                   OBD_CONNECT_ATTRFID  | OBD_CONNECT_GRANT |
308                                   OBD_CONNECT_VERSION  | OBD_CONNECT_BRW_SIZE |
309                                   OBD_CONNECT_SRVLOCK  |
310                                   OBD_CONNECT_MDS_CAPA | OBD_CONNECT_OSS_CAPA |
311                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID     |
312                                   OBD_CONNECT_AT       | OBD_CONNECT_LOV_V3   |
313                                   OBD_CONNECT_VBR | OBD_CONNECT_FULL20 |
314                                   OBD_CONNECT_64BITHASH |
315                                   OBD_CONNECT_EINPROGRESS |
316                                   OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
317                                   OBD_CONNECT_LAYOUTLOCK | OBD_CONNECT_PINGLESS|
318                                   OBD_CONNECT_MAX_EASIZE |
319                                   OBD_CONNECT_FLOCK_DEAD |
320                                   OBD_CONNECT_DISP_STRIPE | OBD_CONNECT_LFSCK |
321                                   OBD_CONNECT_OPEN_BY_FID |
322                                   OBD_CONNECT_DIR_STRIPE |
323                                   OBD_CONNECT_BULK_MBITS | OBD_CONNECT_CKSUM |
324                                   OBD_CONNECT_SUBTREE |
325                                   OBD_CONNECT_MULTIMODRPCS |
326                                   OBD_CONNECT_GRANT_PARAM |
327                                   OBD_CONNECT_SHORTIO | OBD_CONNECT_FLAGS2;
328
329         data->ocd_connect_flags2 = OBD_CONNECT2_DIR_MIGRATE |
330                                    OBD_CONNECT2_SUM_STATFS |
331                                    OBD_CONNECT2_OVERSTRIPING |
332                                    OBD_CONNECT2_FLR |
333                                    OBD_CONNECT2_LOCK_CONVERT |
334                                    OBD_CONNECT2_ARCHIVE_ID_ARRAY |
335                                    OBD_CONNECT2_INC_XID |
336                                    OBD_CONNECT2_LSOM |
337                                    OBD_CONNECT2_ASYNC_DISCARD |
338                                    OBD_CONNECT2_PCC |
339                                    OBD_CONNECT2_CRUSH | OBD_CONNECT2_LSEEK |
340                                    OBD_CONNECT2_GETATTR_PFID |
341                                    OBD_CONNECT2_DOM_LVB |
342                                    OBD_CONNECT2_REP_MBITS |
343                                    OBD_CONNECT2_ATOMIC_OPEN_LOCK;
344
345 #ifdef HAVE_LRU_RESIZE_SUPPORT
346         if (test_bit(LL_SBI_LRU_RESIZE, sbi->ll_flags))
347                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
348 #endif
349         data->ocd_connect_flags |= OBD_CONNECT_ACL_FLAGS;
350
351         data->ocd_cksum_types = obd_cksum_types_supported_client();
352
353         if (OBD_FAIL_CHECK(OBD_FAIL_MDC_LIGHTWEIGHT))
354                 /* flag mdc connection as lightweight, only used for test
355                  * purpose, use with care */
356                 data->ocd_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
357
358         data->ocd_ibits_known = MDS_INODELOCK_FULL;
359         data->ocd_version = LUSTRE_VERSION_CODE;
360
361         if (sb->s_flags & SB_RDONLY)
362                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
363         if (test_bit(LL_SBI_USER_XATTR, sbi->ll_flags))
364                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
365
366 #ifdef SB_NOSEC
367         /* Setting this indicates we correctly support S_NOSEC (See kernel
368          * commit 9e1f1de02c2275d7172e18dc4e7c2065777611bf)
369          */
370         sb->s_flags |= SB_NOSEC;
371 #endif
372         sbi->ll_fop = ll_select_file_operations(sbi);
373
374         /* always ping even if server suppress_pings */
375         if (test_bit(LL_SBI_ALWAYS_PING, sbi->ll_flags))
376                 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
377
378         obd_connect_set_secctx(data);
379         if (ll_sbi_has_encrypt(sbi))
380                 obd_connect_set_enc(data);
381
382 #if defined(CONFIG_SECURITY)
383         data->ocd_connect_flags2 |= OBD_CONNECT2_SELINUX_POLICY;
384 #endif
385
386         data->ocd_brw_size = MD_MAX_BRW_SIZE;
387
388         err = obd_connect(NULL, &sbi->ll_md_exp, sbi->ll_md_obd,
389                           &sbi->ll_sb_uuid, data, sbi->ll_cache);
390         if (err == -EBUSY) {
391                 LCONSOLE_ERROR_MSG(0x14f, "An MDT (md %s) is performing "
392                                    "recovery, of which this client is not a "
393                                    "part. Please wait for recovery to complete,"
394                                    " abort, or time out.\n", md);
395                 GOTO(out, err);
396         } else if (err) {
397                 CERROR("cannot connect to %s: rc = %d\n", md, err);
398                 GOTO(out, err);
399         }
400
401         sbi->ll_md_exp->exp_connect_data = *data;
402
403         err = obd_fid_init(sbi->ll_md_exp->exp_obd, sbi->ll_md_exp,
404                            LUSTRE_SEQ_METADATA);
405         if (err) {
406                 CERROR("%s: Can't init metadata layer FID infrastructure, "
407                        "rc = %d\n", sbi->ll_md_exp->exp_obd->obd_name, err);
408                 GOTO(out_md, err);
409         }
410
411         /* For mount, we only need fs info from MDT0, and also in DNE, it
412          * can make sure the client can be mounted as long as MDT0 is
413          * avaible */
414         err = obd_statfs(NULL, sbi->ll_md_exp, osfs,
415                         ktime_get_seconds() - sbi->ll_statfs_max_age,
416                         OBD_STATFS_FOR_MDT0);
417         if (err)
418                 GOTO(out_md_fid, err);
419
420         /* This needs to be after statfs to ensure connect has finished.
421          * Note that "data" does NOT contain the valid connect reply.
422          * If connecting to a 1.8 server there will be no LMV device, so
423          * we can access the MDC export directly and exp_connect_flags will
424          * be non-zero, but if accessing an upgraded 2.1 server it will
425          * have the correct flags filled in.
426          * XXX: fill in the LMV exp_connect_flags from MDC(s). */
427         valid = exp_connect_flags(sbi->ll_md_exp) & CLIENT_CONNECT_MDT_REQD;
428         if (exp_connect_flags(sbi->ll_md_exp) != 0 &&
429             valid != CLIENT_CONNECT_MDT_REQD) {
430                 char *buf;
431
432                 OBD_ALLOC_WAIT(buf, PAGE_SIZE);
433                 obd_connect_flags2str(buf, PAGE_SIZE,
434                                       valid ^ CLIENT_CONNECT_MDT_REQD, 0, ",");
435                 LCONSOLE_ERROR_MSG(0x170, "Server %s does not support "
436                                    "feature(s) needed for correct operation "
437                                    "of this client (%s). Please upgrade "
438                                    "server or downgrade client.\n",
439                                    sbi->ll_md_exp->exp_obd->obd_name, buf);
440                 OBD_FREE(buf, PAGE_SIZE);
441                 GOTO(out_md_fid, err = -EPROTO);
442         }
443
444         size = sizeof(*data);
445         err = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_CONN_DATA),
446                            KEY_CONN_DATA,  &size, data);
447         if (err) {
448                 CERROR("%s: Get connect data failed: rc = %d\n",
449                        sbi->ll_md_exp->exp_obd->obd_name, err);
450                 GOTO(out_md_fid, err);
451         }
452
453         LASSERT(osfs->os_bsize);
454         sb->s_blocksize = osfs->os_bsize;
455         sb->s_blocksize_bits = log2(osfs->os_bsize);
456         sb->s_magic = LL_SUPER_MAGIC;
457         sb->s_maxbytes = MAX_LFS_FILESIZE;
458         sbi->ll_namelen = osfs->os_namelen;
459         sbi->ll_mnt.mnt = current->fs->root.mnt;
460
461         if (test_bit(LL_SBI_USER_XATTR, sbi->ll_flags) &&
462             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
463                 LCONSOLE_INFO("Disabling user_xattr feature because "
464                               "it is not supported on the server\n");
465                 clear_bit(LL_SBI_USER_XATTR, sbi->ll_flags);
466         }
467
468         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
469 #ifdef SB_POSIXACL
470                 sb->s_flags |= SB_POSIXACL;
471 #endif
472                 set_bit(LL_SBI_ACL, sbi->ll_flags);
473         } else {
474                 LCONSOLE_INFO("client wants to enable acl, but mdt not!\n");
475 #ifdef SB_POSIXACL
476                 sb->s_flags &= ~SB_POSIXACL;
477 #endif
478                 clear_bit(LL_SBI_ACL, sbi->ll_flags);
479         }
480
481         if (data->ocd_connect_flags & OBD_CONNECT_64BITHASH)
482                 set_bit(LL_SBI_64BIT_HASH, sbi->ll_flags);
483
484         if (data->ocd_connect_flags & OBD_CONNECT_LAYOUTLOCK)
485                 set_bit(LL_SBI_LAYOUT_LOCK, sbi->ll_flags);
486
487         if (obd_connect_has_secctx(data))
488                 set_bit(LL_SBI_FILE_SECCTX, sbi->ll_flags);
489
490         if (ll_sbi_has_encrypt(sbi) && !obd_connect_has_enc(data)) {
491                 if (ll_sbi_has_test_dummy_encryption(sbi))
492                         LCONSOLE_WARN("%s: server %s does not support encryption feature, encryption deactivated.\n",
493                                       sbi->ll_fsname,
494                                       sbi->ll_md_exp->exp_obd->obd_name);
495                 ll_sbi_set_encrypt(sbi, false);
496         }
497
498         if (data->ocd_ibits_known & MDS_INODELOCK_XATTR) {
499                 if (!(data->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)) {
500                         LCONSOLE_INFO("%s: disabling xattr cache due to "
501                                       "unknown maximum xattr size.\n", dt);
502                 } else if (!sbi->ll_xattr_cache_set) {
503                         /* If xattr_cache is already set (no matter 0 or 1)
504                          * during processing llog, it won't be enabled here. */
505                         set_bit(LL_SBI_XATTR_CACHE, sbi->ll_flags);
506                         sbi->ll_xattr_cache_enabled = 1;
507                 }
508         }
509
510         sbi->ll_dt_obd = class_name2obd(dt);
511         if (!sbi->ll_dt_obd) {
512                 CERROR("DT %s: not setup or attached\n", dt);
513                 GOTO(out_md_fid, err = -ENODEV);
514         }
515
516         /* pass client page size via ocd_grant_blkbits, the server should report
517          * back its backend blocksize for grant calculation purpose */
518         data->ocd_grant_blkbits = PAGE_SHIFT;
519
520         /* indicate OST features supported by this client */
521         data->ocd_connect_flags = OBD_CONNECT_GRANT | OBD_CONNECT_VERSION |
522                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
523                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID |
524                                   OBD_CONNECT_SRVLOCK |
525                                   OBD_CONNECT_AT | OBD_CONNECT_OSS_CAPA |
526                                   OBD_CONNECT_VBR | OBD_CONNECT_FULL20 |
527                                   OBD_CONNECT_64BITHASH | OBD_CONNECT_MAXBYTES |
528                                   OBD_CONNECT_EINPROGRESS |
529                                   OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
530                                   OBD_CONNECT_LAYOUTLOCK |
531                                   OBD_CONNECT_PINGLESS | OBD_CONNECT_LFSCK |
532                                   OBD_CONNECT_BULK_MBITS | OBD_CONNECT_SHORTIO |
533                                   OBD_CONNECT_FLAGS2 | OBD_CONNECT_GRANT_SHRINK;
534         data->ocd_connect_flags2 = OBD_CONNECT2_LOCKAHEAD |
535                                    OBD_CONNECT2_INC_XID | OBD_CONNECT2_LSEEK |
536                                    OBD_CONNECT2_REP_MBITS;
537
538         if (!OBD_FAIL_CHECK(OBD_FAIL_OSC_CONNECT_GRANT_PARAM))
539                 data->ocd_connect_flags |= OBD_CONNECT_GRANT_PARAM;
540
541         /* OBD_CONNECT_CKSUM should always be set, even if checksums are
542          * disabled by default, because it can still be enabled on the
543          * fly via /sys. As a consequence, we still need to come to an
544          * agreement on the supported algorithms at connect time
545          */
546         data->ocd_connect_flags |= OBD_CONNECT_CKSUM;
547
548         if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
549                 data->ocd_cksum_types = OBD_CKSUM_ADLER;
550         else
551                 data->ocd_cksum_types = obd_cksum_types_supported_client();
552
553 #ifdef HAVE_LRU_RESIZE_SUPPORT
554         data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
555 #endif
556         /* always ping even if server suppress_pings */
557         if (test_bit(LL_SBI_ALWAYS_PING, sbi->ll_flags))
558                 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
559
560         if (ll_sbi_has_encrypt(sbi))
561                 obd_connect_set_enc(data);
562
563         CDEBUG(D_RPCTRACE, "ocd_connect_flags: %#llx ocd_version: %d "
564                "ocd_grant: %d\n", data->ocd_connect_flags,
565                data->ocd_version, data->ocd_grant);
566
567         sbi->ll_dt_obd->obd_upcall.onu_owner = &sbi->ll_lco;
568         sbi->ll_dt_obd->obd_upcall.onu_upcall = cl_ocd_update;
569
570         data->ocd_brw_size = DT_MAX_BRW_SIZE;
571
572         err = obd_connect(NULL, &sbi->ll_dt_exp, sbi->ll_dt_obd,
573                           &sbi->ll_sb_uuid, data, sbi->ll_cache);
574         if (err == -EBUSY) {
575                 LCONSOLE_ERROR_MSG(0x150, "An OST (dt %s) is performing "
576                                    "recovery, of which this client is not a "
577                                    "part.  Please wait for recovery to "
578                                    "complete, abort, or time out.\n", dt);
579                 GOTO(out_md, err);
580         } else if (err) {
581                 CERROR("%s: Cannot connect to %s: rc = %d\n",
582                        sbi->ll_dt_exp->exp_obd->obd_name, dt, err);
583                 GOTO(out_md, err);
584         }
585
586         if (ll_sbi_has_encrypt(sbi) &&
587             !obd_connect_has_enc(&sbi->ll_dt_obd->u.lov.lov_ocd)) {
588                 if (ll_sbi_has_test_dummy_encryption(sbi))
589                         LCONSOLE_WARN("%s: server %s does not support encryption feature, encryption deactivated.\n",
590                                       sbi->ll_fsname, dt);
591                 ll_sbi_set_encrypt(sbi, false);
592         } else if (ll_sbi_has_test_dummy_encryption(sbi)) {
593                 LCONSOLE_WARN("Test dummy encryption mode enabled\n");
594         }
595
596         sbi->ll_dt_exp->exp_connect_data = *data;
597
598         /* Don't change value if it was specified in the config log */
599         if (sbi->ll_ra_info.ra_max_read_ahead_whole_pages == -1) {
600                 sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
601                         max_t(unsigned long, SBI_DEFAULT_READ_AHEAD_WHOLE_MAX,
602                               (data->ocd_brw_size >> PAGE_SHIFT));
603                 if (sbi->ll_ra_info.ra_max_read_ahead_whole_pages >
604                     sbi->ll_ra_info.ra_max_pages_per_file)
605                         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
606                                 sbi->ll_ra_info.ra_max_pages_per_file;
607         }
608
609         err = obd_fid_init(sbi->ll_dt_exp->exp_obd, sbi->ll_dt_exp,
610                            LUSTRE_SEQ_METADATA);
611         if (err) {
612                 CERROR("%s: Can't init data layer FID infrastructure, "
613                        "rc = %d\n", sbi->ll_dt_exp->exp_obd->obd_name, err);
614                 GOTO(out_dt, err);
615         }
616
617         mutex_lock(&sbi->ll_lco.lco_lock);
618         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
619         sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
620         sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
621         mutex_unlock(&sbi->ll_lco.lco_lock);
622
623         fid_zero(&sbi->ll_root_fid);
624         err = md_get_root(sbi->ll_md_exp, get_mount_fileset(sb),
625                            &sbi->ll_root_fid);
626         if (err) {
627                 CERROR("cannot mds_connect: rc = %d\n", err);
628                 GOTO(out_lock_cn_cb, err);
629         }
630         if (!fid_is_sane(&sbi->ll_root_fid)) {
631                 CERROR("%s: Invalid root fid "DFID" during mount\n",
632                        sbi->ll_md_exp->exp_obd->obd_name,
633                        PFID(&sbi->ll_root_fid));
634                 GOTO(out_lock_cn_cb, err = -EINVAL);
635         }
636         CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
637
638         sb->s_op = &lustre_super_operations;
639         sb->s_xattr = ll_xattr_handlers;
640 #if THREAD_SIZE >= 8192 /*b=17630*/
641         sb->s_export_op = &lustre_export_operations;
642 #endif
643 #ifdef HAVE_LUSTRE_CRYPTO
644         llcrypt_set_ops(sb, &lustre_cryptops);
645 #endif
646
647         /* make root inode
648          * XXX: move this to after cbd setup? */
649         valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS | OBD_MD_FLMODEASIZE;
650         if (test_bit(LL_SBI_ACL, sbi->ll_flags))
651                 valid |= OBD_MD_FLACL;
652
653         OBD_ALLOC_PTR(op_data);
654         if (op_data == NULL)
655                 GOTO(out_lock_cn_cb, err = -ENOMEM);
656
657         op_data->op_fid1 = sbi->ll_root_fid;
658         op_data->op_mode = 0;
659         op_data->op_valid = valid;
660
661         err = md_getattr(sbi->ll_md_exp, op_data, &request);
662
663         OBD_FREE_PTR(op_data);
664         if (err) {
665                 CERROR("%s: md_getattr failed for root: rc = %d\n",
666                        sbi->ll_md_exp->exp_obd->obd_name, err);
667                 GOTO(out_lock_cn_cb, err);
668         }
669
670         err = md_get_lustre_md(sbi->ll_md_exp, &request->rq_pill,
671                                sbi->ll_dt_exp, sbi->ll_md_exp, &lmd);
672         if (err) {
673                 CERROR("failed to understand root inode md: rc = %d\n", err);
674                 ptlrpc_req_finished(request);
675                 GOTO(out_lock_cn_cb, err);
676         }
677
678         LASSERT(fid_is_sane(&sbi->ll_root_fid));
679         api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
680         root = ll_iget(sb, cl_fid_build_ino(&sbi->ll_root_fid, api32), &lmd);
681         md_free_lustre_md(sbi->ll_md_exp, &lmd);
682         ptlrpc_req_finished(request);
683
684         if (IS_ERR(root)) {
685                 lmd_clear_acl(&lmd);
686                 err = IS_ERR(root) ? PTR_ERR(root) : -EBADF;
687                 root = NULL;
688                 CERROR("%s: bad ll_iget() for root: rc = %d\n",
689                        sbi->ll_fsname, err);
690                 GOTO(out_root, err);
691         }
692
693         checksum = test_bit(LL_SBI_CHECKSUM, sbi->ll_flags);
694         if (sbi->ll_checksum_set) {
695                 err = obd_set_info_async(NULL, sbi->ll_dt_exp,
696                                          sizeof(KEY_CHECKSUM), KEY_CHECKSUM,
697                                          sizeof(checksum), &checksum, NULL);
698                 if (err) {
699                         CERROR("%s: Set checksum failed: rc = %d\n",
700                                sbi->ll_dt_exp->exp_obd->obd_name, err);
701                         GOTO(out_root, err);
702                 }
703         }
704         cl_sb_init(sb);
705
706         sb->s_root = d_make_root(root);
707         if (sb->s_root == NULL) {
708                 err = -ENOMEM;
709                 CERROR("%s: can't make root dentry: rc = %d\n",
710                        sbi->ll_fsname, err);
711                 GOTO(out_root, err);
712         }
713
714         sbi->ll_sdev_orig = sb->s_dev;
715
716         /* We set sb->s_dev equal on all lustre clients in order to support
717          * NFS export clustering.  NFSD requires that the FSID be the same
718          * on all clients. */
719         /* s_dev is also used in lt_compare() to compare two fs, but that is
720          * only a node-local comparison. */
721         uuid = obd_get_uuid(sbi->ll_md_exp);
722         if (uuid != NULL)
723                 sb->s_dev = get_uuid2int(uuid->uuid, strlen(uuid->uuid));
724
725         if (data != NULL)
726                 OBD_FREE_PTR(data);
727         if (osfs != NULL)
728                 OBD_FREE_PTR(osfs);
729
730         if (sbi->ll_dt_obd) {
731                 err = sysfs_create_link(&sbi->ll_kset.kobj,
732                                         &sbi->ll_dt_obd->obd_kset.kobj,
733                                         sbi->ll_dt_obd->obd_type->typ_name);
734                 if (err < 0) {
735                         CERROR("%s: could not register %s in llite: rc = %d\n",
736                                dt, sbi->ll_fsname, err);
737                         err = 0;
738                 }
739         }
740
741         if (sbi->ll_md_obd) {
742                 err = sysfs_create_link(&sbi->ll_kset.kobj,
743                                         &sbi->ll_md_obd->obd_kset.kobj,
744                                         sbi->ll_md_obd->obd_type->typ_name);
745                 if (err < 0) {
746                         CERROR("%s: could not register %s in llite: rc = %d\n",
747                                md, sbi->ll_fsname, err);
748                         err = 0;
749                 }
750         }
751
752         RETURN(err);
753 out_root:
754         iput(root);
755 out_lock_cn_cb:
756         obd_fid_fini(sbi->ll_dt_exp->exp_obd);
757 out_dt:
758         obd_disconnect(sbi->ll_dt_exp);
759         sbi->ll_dt_exp = NULL;
760         sbi->ll_dt_obd = NULL;
761 out_md_fid:
762         obd_fid_fini(sbi->ll_md_exp->exp_obd);
763 out_md:
764         obd_disconnect(sbi->ll_md_exp);
765         sbi->ll_md_exp = NULL;
766         sbi->ll_md_obd = NULL;
767 out:
768         if (data != NULL)
769                 OBD_FREE_PTR(data);
770         if (osfs != NULL)
771                 OBD_FREE_PTR(osfs);
772         return err;
773 }
774
775 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
776 {
777         int size, rc;
778
779         size = sizeof(*lmmsize);
780         rc = obd_get_info(NULL, sbi->ll_dt_exp, sizeof(KEY_MAX_EASIZE),
781                           KEY_MAX_EASIZE, &size, lmmsize);
782         if (rc != 0) {
783                 CERROR("%s: cannot get max LOV EA size: rc = %d\n",
784                        sbi->ll_dt_exp->exp_obd->obd_name, rc);
785                 RETURN(rc);
786         }
787
788         CDEBUG(D_INFO, "max LOV ea size: %d\n", *lmmsize);
789
790         size = sizeof(int);
791         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_MAX_EASIZE),
792                           KEY_MAX_EASIZE, &size, lmmsize);
793         if (rc)
794                 CERROR("Get max mdsize error rc %d\n", rc);
795
796         CDEBUG(D_INFO, "max LMV ea size: %d\n", *lmmsize);
797
798         RETURN(rc);
799 }
800
801 /**
802  * Get the value of the default_easize parameter.
803  *
804  * \see client_obd::cl_default_mds_easize
805  *
806  * \param[in] sbi       superblock info for this filesystem
807  * \param[out] lmmsize  pointer to storage location for value
808  *
809  * \retval 0            on success
810  * \retval negative     negated errno on failure
811  */
812 int ll_get_default_mdsize(struct ll_sb_info *sbi, int *lmmsize)
813 {
814         int size, rc;
815
816         size = sizeof(int);
817         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_DEFAULT_EASIZE),
818                          KEY_DEFAULT_EASIZE, &size, lmmsize);
819         if (rc)
820                 CERROR("Get default mdsize error rc %d\n", rc);
821
822         RETURN(rc);
823 }
824
825 /**
826  * Set the default_easize parameter to the given value.
827  *
828  * \see client_obd::cl_default_mds_easize
829  *
830  * \param[in] sbi       superblock info for this filesystem
831  * \param[in] lmmsize   the size to set
832  *
833  * \retval 0            on success
834  * \retval negative     negated errno on failure
835  */
836 int ll_set_default_mdsize(struct ll_sb_info *sbi, int lmmsize)
837 {
838         int rc;
839
840         if (lmmsize < sizeof(struct lov_mds_md) ||
841             lmmsize > OBD_MAX_DEFAULT_EA_SIZE)
842                 return -EINVAL;
843
844         rc = obd_set_info_async(NULL, sbi->ll_md_exp,
845                                 sizeof(KEY_DEFAULT_EASIZE), KEY_DEFAULT_EASIZE,
846                                 sizeof(int), &lmmsize, NULL);
847
848         RETURN(rc);
849 }
850
851 static void client_common_put_super(struct super_block *sb)
852 {
853         struct ll_sb_info *sbi = ll_s2sbi(sb);
854         ENTRY;
855
856         cl_sb_fini(sb);
857
858         obd_fid_fini(sbi->ll_dt_exp->exp_obd);
859         obd_disconnect(sbi->ll_dt_exp);
860         sbi->ll_dt_exp = NULL;
861
862         ll_debugfs_unregister_super(sb);
863
864         obd_fid_fini(sbi->ll_md_exp->exp_obd);
865         obd_disconnect(sbi->ll_md_exp);
866         sbi->ll_md_exp = NULL;
867
868         EXIT;
869 }
870
871 void ll_kill_super(struct super_block *sb)
872 {
873         struct ll_sb_info *sbi;
874         ENTRY;
875
876         /* not init sb ?*/
877         if (!(sb->s_flags & SB_ACTIVE))
878                 return;
879
880         sbi = ll_s2sbi(sb);
881         /* we need restore s_dev from changed for clustred NFS before put_super
882          * because new kernels have cached s_dev and change sb->s_dev in
883          * put_super not affected real removing devices */
884         if (sbi) {
885                 sb->s_dev = sbi->ll_sdev_orig;
886
887                 /* wait running statahead threads to quit */
888                 while (atomic_read(&sbi->ll_sa_running) > 0)
889                         schedule_timeout_uninterruptible(
890                                 cfs_time_seconds(1) >> 3);
891         }
892
893         EXIT;
894 }
895
896 /* Since we use this table for ll_sbi_flags_seq_show make
897  * sure what you want displayed for a specific token that
898  * is listed more than once below be listed first. For
899  * example we want "checksum" displayed, not "nochecksum"
900  * for the sbi_flags.
901  */
902 static const match_table_t ll_sbi_flags_name = {
903         {LL_SBI_NOLCK,                  "nolock"},
904         {LL_SBI_CHECKSUM,               "checksum"},
905         {LL_SBI_CHECKSUM,               "nochecksum"},
906         {LL_SBI_LOCALFLOCK,             "localflock"},
907         {LL_SBI_FLOCK,                  "flock"},
908         {LL_SBI_FLOCK,                  "noflock"},
909         {LL_SBI_USER_XATTR,             "user_xattr"},
910         {LL_SBI_USER_XATTR,             "nouser_xattr"},
911         {LL_SBI_LRU_RESIZE,             "lruresize"},
912         {LL_SBI_LRU_RESIZE,             "nolruresize"},
913         {LL_SBI_LAZYSTATFS,             "lazystatfs"},
914         {LL_SBI_LAZYSTATFS,             "nolazystatfs"},
915         {LL_SBI_32BIT_API,              "32bitapi"},
916         {LL_SBI_USER_FID2PATH,          "user_fid2path"},
917         {LL_SBI_USER_FID2PATH,          "nouser_fid2path"},
918         {LL_SBI_VERBOSE,                "verbose"},
919         {LL_SBI_VERBOSE,                "noverbose"},
920         {LL_SBI_ALWAYS_PING,            "always_ping"},
921         {LL_SBI_TEST_DUMMY_ENCRYPTION,  "test_dummy_encryption"},
922         {LL_SBI_ENCRYPT,                "encrypt"},
923         {LL_SBI_ENCRYPT,                "noencrypt"},
924         {LL_SBI_FOREIGN_SYMLINK,        "foreign_symlink=%s"},
925         {LL_SBI_NUM_MOUNT_OPT,          NULL},
926
927         {LL_SBI_ACL,                    "acl"},
928         {LL_SBI_AGL_ENABLED,            "agl"},
929         {LL_SBI_64BIT_HASH,             "64bit_hash"},
930         {LL_SBI_LAYOUT_LOCK,            "layout"},
931         {LL_SBI_XATTR_CACHE,            "xattr_cache"},
932         {LL_SBI_NOROOTSQUASH,           "norootsquash"},
933         {LL_SBI_FAST_READ,              "fast_read"},
934         {LL_SBI_FILE_SECCTX,            "file_secctx"},
935         {LL_SBI_TINY_WRITE,             "tiny_write"},
936         {LL_SBI_FILE_HEAT,              "file_heat"},
937         {LL_SBI_PARALLEL_DIO,           "parallel_dio"},
938 };
939
940 int ll_sbi_flags_seq_show(struct seq_file *m, void *v)
941 {
942         struct super_block *sb = m->private;
943         int i;
944
945         for (i = 0; i < LL_SBI_NUM_FLAGS; i++) {
946                 int j;
947
948                 if (!test_bit(i, ll_s2sbi(sb)->ll_flags))
949                         continue;
950
951                 for (j = 0; j < ARRAY_SIZE(ll_sbi_flags_name); j++) {
952                         if (ll_sbi_flags_name[j].token == i &&
953                             ll_sbi_flags_name[j].pattern) {
954                                 seq_printf(m, "%s ",
955                                            ll_sbi_flags_name[j].pattern);
956                                 break;
957                         }
958                 }
959         }
960         seq_puts(m, "\b\n");
961         return 0;
962 }
963
964 /* non-client-specific mount options are parsed in lmd_parse */
965 static int ll_options(char *options, struct super_block *sb)
966 {
967         struct ll_sb_info *sbi = ll_s2sbi(sb);
968         char *s2, *s1, *opts;
969
970         ENTRY;
971         if (!options)
972                 RETURN(0);
973
974         /* Don't stomp on lmd_opts */
975         opts = kstrdup(options, GFP_KERNEL);
976         if (!opts)
977                 RETURN(-ENOMEM);
978         s1 = opts;
979         s2 = opts;
980
981         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
982
983         while ((s1 = strsep(&opts, ",")) != NULL) {
984                 substring_t args[MAX_OPT_ARGS];
985                 bool turn_off = false;
986                 int token;
987
988                 if (!*s1)
989                         continue;
990
991                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
992
993                 if (strncmp(s1, "no", 2) == 0)
994                         turn_off = true;
995
996                 /*
997                  * Initialize args struct so we know whether arg was
998                  * found; some options take optional arguments.
999                  */
1000                 args[0].to = NULL;
1001                 args[0].from = NULL;
1002                 token = match_token(s1, ll_sbi_flags_name, args);
1003                 if (token == LL_SBI_NUM_MOUNT_OPT) {
1004                         if (match_wildcard("context", s1) ||
1005                             match_wildcard("fscontext", s1) ||
1006                             match_wildcard("defcontext", s1) ||
1007                             match_wildcard("rootcontext",s1))
1008                                 continue;
1009
1010                         LCONSOLE_ERROR_MSG(0x152,
1011                                            "Unknown option '%s', won't mount.\n",
1012                                            s1);
1013                         RETURN(-EINVAL);
1014                 }
1015
1016                 switch (token) {
1017                 case LL_SBI_NOLCK:
1018                 case LL_SBI_32BIT_API:
1019                 case LL_SBI_64BIT_HASH:
1020                 case LL_SBI_ALWAYS_PING:
1021                         set_bit(token, sbi->ll_flags);
1022                         break;
1023
1024                 case LL_SBI_FLOCK:
1025                         clear_bit(LL_SBI_LOCALFLOCK, sbi->ll_flags);
1026                         if (turn_off)
1027                                 clear_bit(LL_SBI_FLOCK, sbi->ll_flags);
1028                         else
1029                                 set_bit(token, sbi->ll_flags);
1030                         break;
1031
1032                 case LL_SBI_LOCALFLOCK:
1033                         clear_bit(LL_SBI_FLOCK, sbi->ll_flags);
1034                         set_bit(token, sbi->ll_flags);
1035                         break;
1036
1037                 case LL_SBI_CHECKSUM:
1038                         sbi->ll_checksum_set = 1;
1039                         /* fall through */
1040                 case LL_SBI_USER_XATTR:
1041                 case LL_SBI_USER_FID2PATH:
1042                 case LL_SBI_LRU_RESIZE:
1043                 case LL_SBI_LAZYSTATFS:
1044                 case LL_SBI_VERBOSE:
1045                         if (turn_off)
1046                                 clear_bit(token, sbi->ll_flags);
1047                         else
1048                                 set_bit(token, sbi->ll_flags);
1049                         break;
1050                 case LL_SBI_TEST_DUMMY_ENCRYPTION: {
1051 #ifdef HAVE_LUSTRE_CRYPTO
1052                         set_bit(token, sbi->ll_flags);
1053 #else
1054                         LCONSOLE_WARN("Test dummy encryption mount option ignored: encryption not supported\n");
1055 #endif
1056                         break;
1057                 }
1058                 case LL_SBI_ENCRYPT:
1059 #ifdef HAVE_LUSTRE_CRYPTO
1060                         if (turn_off)
1061                                 clear_bit(token, sbi->ll_flags);
1062                         else
1063                                 set_bit(token, sbi->ll_flags);
1064 #else
1065                         LCONSOLE_WARN("noencrypt or encrypt mount option ignored: encryption not supported\n");
1066 #endif
1067                         break;
1068                 case LL_SBI_FOREIGN_SYMLINK:
1069                         /* non-default prefix provided ? */
1070                         if (args->from) {
1071                                 size_t old_len;
1072                                 char *old;
1073
1074                                 /* path must be absolute */
1075                                 if (args->from[0] != '/') {
1076                                         LCONSOLE_ERROR_MSG(0x152,
1077                                                            "foreign prefix '%s' must be an absolute path\n",
1078                                                            args->from);
1079                                         RETURN(-EINVAL);
1080                                 }
1081
1082                                 old_len = sbi->ll_foreign_symlink_prefix_size;
1083                                 old = sbi->ll_foreign_symlink_prefix;
1084                                 /* alloc for path length and '\0' */
1085                                 sbi->ll_foreign_symlink_prefix = match_strdup(args);
1086                                 if (!sbi->ll_foreign_symlink_prefix) {
1087                                         /* restore previous */
1088                                         sbi->ll_foreign_symlink_prefix = old;
1089                                         sbi->ll_foreign_symlink_prefix_size =
1090                                                 old_len;
1091                                         RETURN(-ENOMEM);
1092                                 }
1093                                 sbi->ll_foreign_symlink_prefix_size =
1094                                         args->to - args->from + 1;
1095                                 OBD_ALLOC_POST(sbi->ll_foreign_symlink_prefix,
1096                                                sbi->ll_foreign_symlink_prefix_size,
1097                                                "kmalloced");
1098                                 if (old)
1099                                         OBD_FREE(old, old_len);
1100
1101                                 /* enable foreign symlink support */
1102                                 set_bit(token, sbi->ll_flags);
1103                         } else {
1104                                 LCONSOLE_ERROR_MSG(0x152,
1105                                                    "invalid %s option\n", s1);
1106                         }
1107                 /* fall through */
1108                 default:
1109                         break;
1110                 }
1111         }
1112         kfree(opts);
1113         RETURN(0);
1114 }
1115
1116 void ll_lli_init(struct ll_inode_info *lli)
1117 {
1118         lli->lli_inode_magic = LLI_INODE_MAGIC;
1119         lli->lli_flags = 0;
1120         rwlock_init(&lli->lli_lock);
1121         lli->lli_posix_acl = NULL;
1122         /* Do not set lli_fid, it has been initialized already. */
1123         fid_zero(&lli->lli_pfid);
1124         lli->lli_mds_read_och = NULL;
1125         lli->lli_mds_write_och = NULL;
1126         lli->lli_mds_exec_och = NULL;
1127         lli->lli_open_fd_read_count = 0;
1128         lli->lli_open_fd_write_count = 0;
1129         lli->lli_open_fd_exec_count = 0;
1130         mutex_init(&lli->lli_och_mutex);
1131         spin_lock_init(&lli->lli_agl_lock);
1132         spin_lock_init(&lli->lli_layout_lock);
1133         ll_layout_version_set(lli, CL_LAYOUT_GEN_NONE);
1134         lli->lli_clob = NULL;
1135
1136         init_rwsem(&lli->lli_xattrs_list_rwsem);
1137         mutex_init(&lli->lli_xattrs_enq_lock);
1138
1139         LASSERT(lli->lli_vfs_inode.i_mode != 0);
1140         if (S_ISDIR(lli->lli_vfs_inode.i_mode)) {
1141                 lli->lli_opendir_key = NULL;
1142                 lli->lli_sai = NULL;
1143                 spin_lock_init(&lli->lli_sa_lock);
1144                 lli->lli_opendir_pid = 0;
1145                 lli->lli_sa_enabled = 0;
1146                 init_rwsem(&lli->lli_lsm_sem);
1147         } else {
1148                 mutex_init(&lli->lli_size_mutex);
1149                 mutex_init(&lli->lli_setattr_mutex);
1150                 lli->lli_symlink_name = NULL;
1151                 ll_trunc_sem_init(&lli->lli_trunc_sem);
1152                 range_lock_tree_init(&lli->lli_write_tree);
1153                 init_rwsem(&lli->lli_glimpse_sem);
1154                 lli->lli_glimpse_time = ktime_set(0, 0);
1155                 INIT_LIST_HEAD(&lli->lli_agl_list);
1156                 lli->lli_agl_index = 0;
1157                 lli->lli_async_rc = 0;
1158                 spin_lock_init(&lli->lli_heat_lock);
1159                 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
1160                 lli->lli_heat_flags = 0;
1161                 mutex_init(&lli->lli_pcc_lock);
1162                 lli->lli_pcc_state = PCC_STATE_FL_NONE;
1163                 lli->lli_pcc_inode = NULL;
1164                 lli->lli_pcc_dsflags = PCC_DATASET_INVALID;
1165                 lli->lli_pcc_generation = 0;
1166                 mutex_init(&lli->lli_group_mutex);
1167                 lli->lli_group_users = 0;
1168                 lli->lli_group_gid = 0;
1169         }
1170         mutex_init(&lli->lli_layout_mutex);
1171         memset(lli->lli_jobid, 0, sizeof(lli->lli_jobid));
1172         /* ll_cl_context initialize */
1173         INIT_LIST_HEAD(&lli->lli_lccs);
1174 }
1175
1176 #define MAX_STRING_SIZE 128
1177
1178 #ifndef HAVE_SUPER_SETUP_BDI_NAME
1179
1180 #define LSI_BDI_INITIALIZED     0x00400000
1181
1182 #ifndef HAVE_BDI_CAP_MAP_COPY
1183 # define BDI_CAP_MAP_COPY       0
1184 #endif
1185
1186 static int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
1187 {
1188         struct  lustre_sb_info *lsi = s2lsi(sb);
1189         char buf[MAX_STRING_SIZE];
1190         va_list args;
1191         int err;
1192
1193         err = bdi_init(&lsi->lsi_bdi);
1194         if (err)
1195                 return err;
1196
1197         lsi->lsi_flags |= LSI_BDI_INITIALIZED;
1198         lsi->lsi_bdi.capabilities = BDI_CAP_MAP_COPY;
1199         lsi->lsi_bdi.name = "lustre";
1200         va_start(args, fmt);
1201         vsnprintf(buf, MAX_STRING_SIZE, fmt, args);
1202         va_end(args);
1203         err = bdi_register(&lsi->lsi_bdi, NULL, "%s", buf);
1204         va_end(args);
1205         if (!err)
1206                 sb->s_bdi = &lsi->lsi_bdi;
1207
1208         return err;
1209 }
1210 #endif /* !HAVE_SUPER_SETUP_BDI_NAME */
1211
1212 int ll_fill_super(struct super_block *sb)
1213 {
1214         struct  lustre_profile *lprof = NULL;
1215         struct  lustre_sb_info *lsi = s2lsi(sb);
1216         struct  ll_sb_info *sbi = NULL;
1217         char    *dt = NULL, *md = NULL;
1218         char    *profilenm = get_profile_name(sb);
1219         struct config_llog_instance *cfg;
1220         /* %p for void* in printf needs 16+2 characters: 0xffffffffffffffff */
1221         const int instlen = LUSTRE_MAXINSTANCE + 2;
1222         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1223         char name[MAX_STRING_SIZE];
1224         int md_len = 0;
1225         int dt_len = 0;
1226         uuid_t uuid;
1227         char *ptr;
1228         int len;
1229         int err;
1230
1231         ENTRY;
1232         /* for ASLR, to map between cfg_instance and hashed ptr */
1233         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1234                profilenm, cfg_instance, sb);
1235
1236         OBD_RACE(OBD_FAIL_LLITE_RACE_MOUNT);
1237
1238         OBD_ALLOC_PTR(cfg);
1239         if (cfg == NULL)
1240                 GOTO(out_free_cfg, err = -ENOMEM);
1241
1242         /* client additional sb info */
1243         lsi->lsi_llsbi = sbi = ll_init_sbi();
1244         if (IS_ERR(sbi))
1245                 GOTO(out_free_cfg, err = PTR_ERR(sbi));
1246
1247         err = ll_options(lsi->lsi_lmd->lmd_opts, sb);
1248         if (err)
1249                 GOTO(out_free_cfg, err);
1250
1251         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
1252         sb->s_d_op = &ll_d_ops;
1253
1254         /* UUID handling */
1255         generate_random_uuid(uuid.b);
1256         snprintf(sbi->ll_sb_uuid.uuid, sizeof(sbi->ll_sb_uuid), "%pU", uuid.b);
1257
1258         CDEBUG(D_CONFIG, "llite sb uuid: %s\n", sbi->ll_sb_uuid.uuid);
1259
1260         /* Get fsname */
1261         len = strlen(profilenm);
1262         ptr = strrchr(profilenm, '-');
1263         if (ptr && (strcmp(ptr, "-client") == 0))
1264                 len -= 7;
1265
1266         if (len > LUSTRE_MAXFSNAME) {
1267                 if (unlikely(len >= MAX_STRING_SIZE))
1268                         len = MAX_STRING_SIZE - 1;
1269                 strncpy(name, profilenm, len);
1270                 name[len] = '\0';
1271                 err = -ENAMETOOLONG;
1272                 CERROR("%s: fsname longer than %u characters: rc = %d\n",
1273                        name, LUSTRE_MAXFSNAME, err);
1274                 GOTO(out_free_cfg, err);
1275         }
1276         strncpy(sbi->ll_fsname, profilenm, len);
1277         sbi->ll_fsname[len] = '\0';
1278
1279         /* Mount info */
1280         snprintf(name, sizeof(name), "%.*s-%016lx", len,
1281                  profilenm, cfg_instance);
1282
1283         err = super_setup_bdi_name(sb, "%s", name);
1284         if (err)
1285                 GOTO(out_free_cfg, err);
1286
1287         /* Call ll_debugfs_register_super() before lustre_process_log()
1288          * so that "llite.*.*" params can be processed correctly.
1289          */
1290         err = ll_debugfs_register_super(sb, name);
1291         if (err < 0) {
1292                 CERROR("%s: could not register mountpoint in llite: rc = %d\n",
1293                        sbi->ll_fsname, err);
1294                 err = 0;
1295         }
1296
1297         /* The cfg_instance is a value unique to this super, in case some
1298          * joker tries to mount the same fs at two mount points.
1299          */
1300         cfg->cfg_instance = cfg_instance;
1301         cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1302         cfg->cfg_callback = class_config_llog_handler;
1303         cfg->cfg_sub_clds = CONFIG_SUB_CLIENT;
1304         /* set up client obds */
1305         err = lustre_process_log(sb, profilenm, cfg);
1306         if (err < 0)
1307                 GOTO(out_debugfs, err);
1308
1309         /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
1310         lprof = class_get_profile(profilenm);
1311         if (lprof == NULL) {
1312                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1313                                    " read from the MGS.  Does that filesystem "
1314                                    "exist?\n", profilenm);
1315                 GOTO(out_debugfs, err = -EINVAL);
1316         }
1317         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1318                lprof->lp_md, lprof->lp_dt);
1319
1320         dt_len = strlen(lprof->lp_dt) + instlen + 2;
1321         OBD_ALLOC(dt, dt_len);
1322         if (!dt)
1323                 GOTO(out_profile, err = -ENOMEM);
1324         snprintf(dt, dt_len - 1, "%s-%016lx", lprof->lp_dt, cfg_instance);
1325
1326         md_len = strlen(lprof->lp_md) + instlen + 2;
1327         OBD_ALLOC(md, md_len);
1328         if (!md)
1329                 GOTO(out_free_dt, err = -ENOMEM);
1330         snprintf(md, md_len - 1, "%s-%016lx", lprof->lp_md, cfg_instance);
1331
1332         /* connections, registrations, sb setup */
1333         err = client_common_fill_super(sb, md, dt);
1334         if (err < 0)
1335                 GOTO(out_free_md, err);
1336
1337         sbi->ll_client_common_fill_super_succeeded = 1;
1338
1339 out_free_md:
1340         if (md)
1341                 OBD_FREE(md, md_len);
1342 out_free_dt:
1343         if (dt)
1344                 OBD_FREE(dt, dt_len);
1345 out_profile:
1346         if (lprof)
1347                 class_put_profile(lprof);
1348 out_debugfs:
1349         if (err < 0)
1350                 ll_debugfs_unregister_super(sb);
1351 out_free_cfg:
1352         if (cfg)
1353                 OBD_FREE_PTR(cfg);
1354
1355         if (err)
1356                 ll_put_super(sb);
1357         else if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
1358                 LCONSOLE_WARN("Mounted %s\n", profilenm);
1359         RETURN(err);
1360 } /* ll_fill_super */
1361
1362 void ll_put_super(struct super_block *sb)
1363 {
1364         struct config_llog_instance cfg, params_cfg;
1365         struct obd_device *obd;
1366         struct lustre_sb_info *lsi = s2lsi(sb);
1367         struct ll_sb_info *sbi = ll_s2sbi(sb);
1368         char *profilenm = get_profile_name(sb);
1369         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1370         long ccc_count;
1371         int next, force = 1, rc = 0;
1372         ENTRY;
1373
1374         if (IS_ERR(sbi))
1375                 GOTO(out_no_sbi, 0);
1376
1377         /* Should replace instance_id with something better for ASLR */
1378         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1379                profilenm, cfg_instance, sb);
1380
1381         cfg.cfg_instance = cfg_instance;
1382         lustre_end_log(sb, profilenm, &cfg);
1383
1384         params_cfg.cfg_instance = cfg_instance;
1385         lustre_end_log(sb, PARAMS_FILENAME, &params_cfg);
1386
1387         if (sbi->ll_md_exp) {
1388                 obd = class_exp2obd(sbi->ll_md_exp);
1389                 if (obd)
1390                         force = obd->obd_force;
1391         }
1392
1393         /* Wait for unstable pages to be committed to stable storage */
1394         if (force == 0) {
1395                 rc = l_wait_event_abortable(
1396                         sbi->ll_cache->ccc_unstable_waitq,
1397                         atomic_long_read(&sbi->ll_cache->ccc_unstable_nr) == 0);
1398         }
1399
1400         ccc_count = atomic_long_read(&sbi->ll_cache->ccc_unstable_nr);
1401         if (force == 0 && rc != -ERESTARTSYS)
1402                 LASSERTF(ccc_count == 0, "count: %li\n", ccc_count);
1403
1404         /* We need to set force before the lov_disconnect in
1405          * lustre_common_put_super, since l_d cleans up osc's as well.
1406          */
1407         if (force) {
1408                 next = 0;
1409                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1410                                                      &next)) != NULL) {
1411                         obd->obd_force = force;
1412                 }
1413         }
1414
1415         if (sbi->ll_client_common_fill_super_succeeded) {
1416                 /* Only if client_common_fill_super succeeded */
1417                 client_common_put_super(sb);
1418         }
1419
1420         next = 0;
1421         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)))
1422                 class_manual_cleanup(obd);
1423
1424         if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
1425                 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
1426
1427         if (profilenm)
1428                 class_del_profile(profilenm);
1429
1430 #ifndef HAVE_SUPER_SETUP_BDI_NAME
1431         if (lsi->lsi_flags & LSI_BDI_INITIALIZED) {
1432                 bdi_destroy(&lsi->lsi_bdi);
1433                 lsi->lsi_flags &= ~LSI_BDI_INITIALIZED;
1434         }
1435 #endif
1436
1437         ll_free_sbi(sb);
1438         lsi->lsi_llsbi = NULL;
1439 out_no_sbi:
1440         lustre_common_put_super(sb);
1441
1442         cl_env_cache_purge(~0);
1443
1444         EXIT;
1445 } /* client_put_super */
1446
1447 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock)
1448 {
1449         struct inode *inode = NULL;
1450
1451         /* NOTE: we depend on atomic igrab() -bzzz */
1452         lock_res_and_lock(lock);
1453         if (lock->l_resource->lr_lvb_inode) {
1454                 struct ll_inode_info * lli;
1455                 lli = ll_i2info(lock->l_resource->lr_lvb_inode);
1456                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1457                         inode = igrab(lock->l_resource->lr_lvb_inode);
1458                 } else {
1459                         inode = lock->l_resource->lr_lvb_inode;
1460                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1461                                          D_WARNING, lock, "lr_lvb_inode %p is "
1462                                          "bogus: magic %08x",
1463                                          lock->l_resource->lr_lvb_inode,
1464                                          lli->lli_inode_magic);
1465                         inode = NULL;
1466                 }
1467         }
1468         unlock_res_and_lock(lock);
1469         return inode;
1470 }
1471
1472 void ll_dir_clear_lsm_md(struct inode *inode)
1473 {
1474         struct ll_inode_info *lli = ll_i2info(inode);
1475
1476         LASSERT(S_ISDIR(inode->i_mode));
1477
1478         if (lli->lli_lsm_md) {
1479                 lmv_free_memmd(lli->lli_lsm_md);
1480                 lli->lli_lsm_md = NULL;
1481         }
1482
1483         if (lli->lli_default_lsm_md) {
1484                 lmv_free_memmd(lli->lli_default_lsm_md);
1485                 lli->lli_default_lsm_md = NULL;
1486         }
1487 }
1488
1489 static struct inode *ll_iget_anon_dir(struct super_block *sb,
1490                                       const struct lu_fid *fid,
1491                                       struct lustre_md *md)
1492 {
1493         struct ll_sb_info *sbi = ll_s2sbi(sb);
1494         struct ll_inode_info *lli;
1495         struct mdt_body *body = md->body;
1496         struct inode *inode;
1497         ino_t ino;
1498
1499         ENTRY;
1500
1501         LASSERT(md->lmv);
1502         ino = cl_fid_build_ino(fid, test_bit(LL_SBI_32BIT_API, sbi->ll_flags));
1503         inode = iget_locked(sb, ino);
1504         if (inode == NULL) {
1505                 CERROR("%s: failed get simple inode "DFID": rc = -ENOENT\n",
1506                        sbi->ll_fsname, PFID(fid));
1507                 RETURN(ERR_PTR(-ENOENT));
1508         }
1509
1510         lli = ll_i2info(inode);
1511         if (inode->i_state & I_NEW) {
1512                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1513                                 (body->mbo_mode & S_IFMT);
1514                 LASSERTF(S_ISDIR(inode->i_mode), "Not slave inode "DFID"\n",
1515                          PFID(fid));
1516
1517                 inode->i_mtime.tv_sec = 0;
1518                 inode->i_atime.tv_sec = 0;
1519                 inode->i_ctime.tv_sec = 0;
1520                 inode->i_rdev = 0;
1521
1522 #ifdef HAVE_BACKING_DEV_INFO
1523                 /* initializing backing dev info. */
1524                 inode->i_mapping->backing_dev_info =
1525                                                 &s2lsi(inode->i_sb)->lsi_bdi;
1526 #endif
1527                 inode->i_op = &ll_dir_inode_operations;
1528                 inode->i_fop = &ll_dir_operations;
1529                 lli->lli_fid = *fid;
1530                 ll_lli_init(lli);
1531
1532                 /* master object FID */
1533                 lli->lli_pfid = body->mbo_fid1;
1534                 CDEBUG(D_INODE, "lli %p slave "DFID" master "DFID"\n",
1535                        lli, PFID(fid), PFID(&lli->lli_pfid));
1536                 unlock_new_inode(inode);
1537         } else {
1538                 /* in directory restripe/auto-split, a directory will be
1539                  * transformed to a stripe if it's plain, set its pfid here,
1540                  * otherwise ll_lock_cancel_bits() can't find the master inode.
1541                  */
1542                 lli->lli_pfid = body->mbo_fid1;
1543         }
1544
1545         RETURN(inode);
1546 }
1547
1548 static int ll_init_lsm_md(struct inode *inode, struct lustre_md *md)
1549 {
1550         struct lu_fid *fid;
1551         struct lmv_stripe_md *lsm = md->lmv;
1552         struct ll_inode_info *lli = ll_i2info(inode);
1553         int i;
1554
1555         LASSERT(lsm != NULL);
1556
1557         CDEBUG(D_INODE, "%s: "DFID" set dir layout:\n",
1558                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
1559         lsm_md_dump(D_INODE, lsm);
1560
1561         if (!lmv_dir_striped(lsm))
1562                 goto out;
1563
1564         /* XXX sigh, this lsm_root initialization should be in
1565          * LMV layer, but it needs ll_iget right now, so we
1566          * put this here right now. */
1567         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1568                 fid = &lsm->lsm_md_oinfo[i].lmo_fid;
1569                 LASSERT(lsm->lsm_md_oinfo[i].lmo_root == NULL);
1570
1571                 if (!fid_is_sane(fid))
1572                         continue;
1573
1574                 /* Unfortunately ll_iget will call ll_update_inode,
1575                  * where the initialization of slave inode is slightly
1576                  * different, so it reset lsm_md to NULL to avoid
1577                  * initializing lsm for slave inode. */
1578                 lsm->lsm_md_oinfo[i].lmo_root =
1579                                 ll_iget_anon_dir(inode->i_sb, fid, md);
1580                 if (IS_ERR(lsm->lsm_md_oinfo[i].lmo_root)) {
1581                         int rc = PTR_ERR(lsm->lsm_md_oinfo[i].lmo_root);
1582
1583                         lsm->lsm_md_oinfo[i].lmo_root = NULL;
1584                         while (i-- > 0) {
1585                                 iput(lsm->lsm_md_oinfo[i].lmo_root);
1586                                 lsm->lsm_md_oinfo[i].lmo_root = NULL;
1587                         }
1588                         return rc;
1589                 }
1590         }
1591 out:
1592         lli->lli_lsm_md = lsm;
1593
1594         return 0;
1595 }
1596
1597 static void ll_update_default_lsm_md(struct inode *inode, struct lustre_md *md)
1598 {
1599         struct ll_inode_info *lli = ll_i2info(inode);
1600
1601         if (!md->default_lmv) {
1602                 /* clear default lsm */
1603                 if (lli->lli_default_lsm_md) {
1604                         down_write(&lli->lli_lsm_sem);
1605                         if (lli->lli_default_lsm_md) {
1606                                 lmv_free_memmd(lli->lli_default_lsm_md);
1607                                 lli->lli_default_lsm_md = NULL;
1608                         }
1609                         up_write(&lli->lli_lsm_sem);
1610                 }
1611                 return;
1612         }
1613
1614         if (lli->lli_default_lsm_md) {
1615                 /* do nonthing if default lsm isn't changed */
1616                 down_read(&lli->lli_lsm_sem);
1617                 if (lli->lli_default_lsm_md &&
1618                     lsm_md_eq(lli->lli_default_lsm_md, md->default_lmv)) {
1619                         up_read(&lli->lli_lsm_sem);
1620                         return;
1621                 }
1622                 up_read(&lli->lli_lsm_sem);
1623         }
1624
1625         down_write(&lli->lli_lsm_sem);
1626         if (lli->lli_default_lsm_md)
1627                 lmv_free_memmd(lli->lli_default_lsm_md);
1628         lli->lli_default_lsm_md = md->default_lmv;
1629         lsm_md_dump(D_INODE, md->default_lmv);
1630         md->default_lmv = NULL;
1631         up_write(&lli->lli_lsm_sem);
1632 }
1633
1634 static int ll_update_lsm_md(struct inode *inode, struct lustre_md *md)
1635 {
1636         struct ll_inode_info *lli = ll_i2info(inode);
1637         struct lmv_stripe_md *lsm = md->lmv;
1638         struct cl_attr  *attr;
1639         int rc = 0;
1640
1641         ENTRY;
1642
1643         LASSERT(S_ISDIR(inode->i_mode));
1644         CDEBUG(D_INODE, "update lsm %p of "DFID"\n", lli->lli_lsm_md,
1645                PFID(ll_inode2fid(inode)));
1646
1647         /* update default LMV */
1648         if (md->default_lmv)
1649                 ll_update_default_lsm_md(inode, md);
1650
1651         /* after dir migration/restripe, a stripe may be turned into a
1652          * directory, in this case, zero out its lli_pfid.
1653          */
1654         if (unlikely(fid_is_norm(&lli->lli_pfid)))
1655                 fid_zero(&lli->lli_pfid);
1656
1657         /*
1658          * no striped information from request, lustre_md from req does not
1659          * include stripeEA, see ll_md_setattr()
1660          */
1661         if (!lsm)
1662                 RETURN(0);
1663
1664         /*
1665          * normally dir layout doesn't change, only take read lock to check
1666          * that to avoid blocking other MD operations.
1667          */
1668         down_read(&lli->lli_lsm_sem);
1669
1670         /* some current lookup initialized lsm, and unchanged */
1671         if (lli->lli_lsm_md && lsm_md_eq(lli->lli_lsm_md, lsm))
1672                 GOTO(unlock, rc = 0);
1673
1674         /* if dir layout doesn't match, check whether version is increased,
1675          * which means layout is changed, this happens in dir split/merge and
1676          * lfsck.
1677          *
1678          * foreign LMV should not change.
1679          */
1680         if (lli->lli_lsm_md && lmv_dir_striped(lli->lli_lsm_md) &&
1681             lsm->lsm_md_layout_version <=
1682             lli->lli_lsm_md->lsm_md_layout_version) {
1683                 CERROR("%s: "DFID" dir layout mismatch:\n",
1684                        ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
1685                 lsm_md_dump(D_ERROR, lli->lli_lsm_md);
1686                 lsm_md_dump(D_ERROR, lsm);
1687                 GOTO(unlock, rc = -EINVAL);
1688         }
1689
1690         up_read(&lli->lli_lsm_sem);
1691         down_write(&lli->lli_lsm_sem);
1692         /* clear existing lsm */
1693         if (lli->lli_lsm_md) {
1694                 lmv_free_memmd(lli->lli_lsm_md);
1695                 lli->lli_lsm_md = NULL;
1696         }
1697
1698         rc = ll_init_lsm_md(inode, md);
1699         up_write(&lli->lli_lsm_sem);
1700
1701         if (rc)
1702                 RETURN(rc);
1703
1704         /* set md->lmv to NULL, so the following free lustre_md will not free
1705          * this lsm.
1706          */
1707         md->lmv = NULL;
1708
1709         /* md_merge_attr() may take long, since lsm is already set, switch to
1710          * read lock.
1711          */
1712         down_read(&lli->lli_lsm_sem);
1713
1714         if (!lmv_dir_striped(lli->lli_lsm_md))
1715                 GOTO(unlock, rc = 0);
1716
1717         OBD_ALLOC_PTR(attr);
1718         if (!attr)
1719                 GOTO(unlock, rc = -ENOMEM);
1720
1721         /* validate the lsm */
1722         rc = md_merge_attr(ll_i2mdexp(inode), lli->lli_lsm_md, attr,
1723                            ll_md_blocking_ast);
1724         if (!rc) {
1725                 if (md->body->mbo_valid & OBD_MD_FLNLINK)
1726                         md->body->mbo_nlink = attr->cat_nlink;
1727                 if (md->body->mbo_valid & OBD_MD_FLSIZE)
1728                         md->body->mbo_size = attr->cat_size;
1729                 if (md->body->mbo_valid & OBD_MD_FLATIME)
1730                         md->body->mbo_atime = attr->cat_atime;
1731                 if (md->body->mbo_valid & OBD_MD_FLCTIME)
1732                         md->body->mbo_ctime = attr->cat_ctime;
1733                 if (md->body->mbo_valid & OBD_MD_FLMTIME)
1734                         md->body->mbo_mtime = attr->cat_mtime;
1735         }
1736
1737         OBD_FREE_PTR(attr);
1738         GOTO(unlock, rc);
1739 unlock:
1740         up_read(&lli->lli_lsm_sem);
1741
1742         return rc;
1743 }
1744
1745 void ll_clear_inode(struct inode *inode)
1746 {
1747         struct ll_inode_info *lli = ll_i2info(inode);
1748         struct ll_sb_info *sbi = ll_i2sbi(inode);
1749
1750         ENTRY;
1751
1752         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1753                PFID(ll_inode2fid(inode)), inode);
1754
1755         if (S_ISDIR(inode->i_mode)) {
1756                 /* these should have been cleared in ll_file_release */
1757                 LASSERT(lli->lli_opendir_key == NULL);
1758                 LASSERT(lli->lli_sai == NULL);
1759                 LASSERT(lli->lli_opendir_pid == 0);
1760         } else {
1761                 pcc_inode_free(inode);
1762         }
1763
1764         md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1765
1766         LASSERT(!lli->lli_open_fd_write_count);
1767         LASSERT(!lli->lli_open_fd_read_count);
1768         LASSERT(!lli->lli_open_fd_exec_count);
1769
1770         if (lli->lli_mds_write_och)
1771                 ll_md_real_close(inode, FMODE_WRITE);
1772         if (lli->lli_mds_exec_och)
1773                 ll_md_real_close(inode, FMODE_EXEC);
1774         if (lli->lli_mds_read_och)
1775                 ll_md_real_close(inode, FMODE_READ);
1776
1777         if (S_ISLNK(inode->i_mode) && lli->lli_symlink_name) {
1778                 OBD_FREE(lli->lli_symlink_name,
1779                          strlen(lli->lli_symlink_name) + 1);
1780                 lli->lli_symlink_name = NULL;
1781         }
1782
1783         ll_xattr_cache_destroy(inode);
1784
1785         forget_all_cached_acls(inode);
1786         lli_clear_acl(lli);
1787         lli->lli_inode_magic = LLI_INODE_DEAD;
1788
1789         if (S_ISDIR(inode->i_mode))
1790                 ll_dir_clear_lsm_md(inode);
1791         else if (S_ISREG(inode->i_mode) && !is_bad_inode(inode))
1792                 LASSERT(list_empty(&lli->lli_agl_list));
1793
1794         /*
1795          * XXX This has to be done before lsm is freed below, because
1796          * cl_object still uses inode lsm.
1797          */
1798         cl_inode_fini(inode);
1799
1800         llcrypt_put_encryption_info(inode);
1801
1802         EXIT;
1803 }
1804
1805 static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data)
1806 {
1807         struct lustre_md md;
1808         struct inode *inode = dentry->d_inode;
1809         struct ll_sb_info *sbi = ll_i2sbi(inode);
1810         struct ptlrpc_request *request = NULL;
1811         int rc, ia_valid;
1812
1813         ENTRY;
1814
1815         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1816                                      LUSTRE_OPC_ANY, NULL);
1817         if (IS_ERR(op_data))
1818                 RETURN(PTR_ERR(op_data));
1819
1820         /* If this is a chgrp of a regular file, we want to reserve enough
1821          * quota to cover the entire file size.
1822          */
1823         if (S_ISREG(inode->i_mode) && op_data->op_attr.ia_valid & ATTR_GID &&
1824             from_kgid(&init_user_ns, op_data->op_attr.ia_gid) !=
1825             from_kgid(&init_user_ns, inode->i_gid)) {
1826                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
1827                 op_data->op_attr_blocks = inode->i_blocks;
1828         }
1829
1830
1831         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &request);
1832         if (rc) {
1833                 ptlrpc_req_finished(request);
1834                 if (rc == -ENOENT) {
1835                         clear_nlink(inode);
1836                         /* Unlinked special device node? Or just a race?
1837                          * Pretend we done everything. */
1838                         if (!S_ISREG(inode->i_mode) &&
1839                             !S_ISDIR(inode->i_mode)) {
1840                                 ia_valid = op_data->op_attr.ia_valid;
1841                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1842                                 rc = simple_setattr(dentry, &op_data->op_attr);
1843                                 op_data->op_attr.ia_valid = ia_valid;
1844                         }
1845                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1846                         CERROR("md_setattr fails: rc = %d\n", rc);
1847                 }
1848                 RETURN(rc);
1849         }
1850
1851         rc = md_get_lustre_md(sbi->ll_md_exp, &request->rq_pill, sbi->ll_dt_exp,
1852                               sbi->ll_md_exp, &md);
1853         if (rc) {
1854                 ptlrpc_req_finished(request);
1855                 RETURN(rc);
1856         }
1857
1858         ia_valid = op_data->op_attr.ia_valid;
1859         /* inode size will be in ll_setattr_ost, can't do it now since dirty
1860          * cache is not cleared yet. */
1861         op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
1862         if (S_ISREG(inode->i_mode))
1863                 inode_lock(inode);
1864         rc = simple_setattr(dentry, &op_data->op_attr);
1865         if (S_ISREG(inode->i_mode))
1866                 inode_unlock(inode);
1867         op_data->op_attr.ia_valid = ia_valid;
1868
1869         rc = ll_update_inode(inode, &md);
1870         ptlrpc_req_finished(request);
1871
1872         RETURN(rc);
1873 }
1874
1875 /**
1876  * Zero portion of page that is part of @inode.
1877  * This implies, if necessary:
1878  * - taking cl_lock on range corresponding to concerned page
1879  * - grabbing vm page
1880  * - associating cl_page
1881  * - proceeding to clio read
1882  * - zeroing range in page
1883  * - proceeding to cl_page flush
1884  * - releasing cl_lock
1885  *
1886  * \param[in] inode     inode
1887  * \param[in] index     page index
1888  * \param[in] offset    offset in page to start zero from
1889  * \param[in] len       len to zero
1890  *
1891  * \retval 0            on success
1892  * \retval negative     errno on failure
1893  */
1894 int ll_io_zero_page(struct inode *inode, pgoff_t index, pgoff_t offset,
1895                     unsigned len)
1896 {
1897         struct ll_inode_info *lli = ll_i2info(inode);
1898         struct cl_object *clob = lli->lli_clob;
1899         __u16 refcheck;
1900         struct lu_env *env = NULL;
1901         struct cl_io *io = NULL;
1902         struct cl_page *clpage = NULL;
1903         struct page *vmpage = NULL;
1904         unsigned from = index << PAGE_SHIFT;
1905         struct cl_lock *lock = NULL;
1906         struct cl_lock_descr *descr = NULL;
1907         struct cl_2queue *queue = NULL;
1908         struct cl_sync_io *anchor = NULL;
1909         bool holdinglock = false;
1910         bool lockedbymyself = true;
1911         int rc;
1912
1913         ENTRY;
1914
1915         env = cl_env_get(&refcheck);
1916         if (IS_ERR(env))
1917                 RETURN(PTR_ERR(env));
1918
1919         io = vvp_env_thread_io(env);
1920         io->ci_obj = clob;
1921         rc = cl_io_rw_init(env, io, CIT_WRITE, from, PAGE_SIZE);
1922         if (rc)
1923                 GOTO(putenv, rc);
1924
1925         lock = vvp_env_lock(env);
1926         descr = &lock->cll_descr;
1927         descr->cld_obj   = io->ci_obj;
1928         descr->cld_start = cl_index(io->ci_obj, from);
1929         descr->cld_end   = cl_index(io->ci_obj, from + PAGE_SIZE - 1);
1930         descr->cld_mode  = CLM_WRITE;
1931         descr->cld_enq_flags = CEF_MUST | CEF_NONBLOCK;
1932
1933         /* request lock for page */
1934         rc = cl_lock_request(env, io, lock);
1935         /* -ECANCELED indicates a matching lock with a different extent
1936          * was already present, and -EEXIST indicates a matching lock
1937          * on exactly the same extent was already present.
1938          * In both cases it means we are covered.
1939          */
1940         if (rc == -ECANCELED || rc == -EEXIST)
1941                 rc = 0;
1942         else if (rc < 0)
1943                 GOTO(iofini, rc);
1944         else
1945                 holdinglock = true;
1946
1947         /* grab page */
1948         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
1949         if (vmpage == NULL)
1950                 GOTO(rellock, rc = -EOPNOTSUPP);
1951
1952         if (!PageDirty(vmpage)) {
1953                 /* associate cl_page */
1954                 clpage = cl_page_find(env, clob, vmpage->index,
1955                                       vmpage, CPT_CACHEABLE);
1956                 if (IS_ERR(clpage))
1957                         GOTO(pagefini, rc = PTR_ERR(clpage));
1958
1959                 cl_page_assume(env, io, clpage);
1960         }
1961
1962         if (!PageUptodate(vmpage) && !PageDirty(vmpage) &&
1963             !PageWriteback(vmpage)) {
1964                 /* read page */
1965                 /* set PagePrivate2 to detect special case of empty page
1966                  * in osc_brw_fini_request()
1967                  */
1968                 SetPagePrivate2(vmpage);
1969                 rc = ll_io_read_page(env, io, clpage, NULL);
1970                 if (!PagePrivate2(vmpage))
1971                         /* PagePrivate2 was cleared in osc_brw_fini_request()
1972                          * meaning we read an empty page. In this case, in order
1973                          * to avoid allocating unnecessary block in truncated
1974                          * file, we must not zero and write as below. Subsequent
1975                          * server-side truncate will handle things correctly.
1976                          */
1977                         GOTO(clpfini, rc = 0);
1978                 ClearPagePrivate2(vmpage);
1979                 if (rc)
1980                         GOTO(clpfini, rc);
1981                 lockedbymyself = trylock_page(vmpage);
1982                 cl_page_assume(env, io, clpage);
1983         }
1984
1985         /* zero range in page */
1986         zero_user(vmpage, offset, len);
1987
1988         if (holdinglock && clpage) {
1989                 /* explicitly write newly modified page */
1990                 queue = &io->ci_queue;
1991                 cl_2queue_init(queue);
1992                 anchor = &vvp_env_info(env)->vti_anchor;
1993                 cl_sync_io_init(anchor, 1);
1994                 clpage->cp_sync_io = anchor;
1995                 cl_2queue_add(queue, clpage, true);
1996                 rc = cl_io_submit_rw(env, io, CRT_WRITE, queue);
1997                 if (rc)
1998                         GOTO(queuefini1, rc);
1999                 rc = cl_sync_io_wait(env, anchor, 0);
2000                 if (rc)
2001                         GOTO(queuefini2, rc);
2002                 cl_page_assume(env, io, clpage);
2003
2004 queuefini2:
2005                 cl_2queue_discard(env, io, queue);
2006 queuefini1:
2007                 cl_2queue_disown(env, io, queue);
2008                 cl_2queue_fini(env, queue);
2009         }
2010
2011 clpfini:
2012         if (clpage)
2013                 cl_page_put(env, clpage);
2014 pagefini:
2015         if (lockedbymyself) {
2016                 unlock_page(vmpage);
2017                 put_page(vmpage);
2018         }
2019 rellock:
2020         if (holdinglock)
2021                 cl_lock_release(env, lock);
2022 iofini:
2023         cl_io_fini(env, io);
2024 putenv:
2025         if (env)
2026                 cl_env_put(env, &refcheck);
2027
2028         RETURN(rc);
2029 }
2030
2031 /* If this inode has objects allocated to it (lsm != NULL), then the OST
2032  * object(s) determine the file size and mtime.  Otherwise, the MDS will
2033  * keep these values until such a time that objects are allocated for it.
2034  * We do the MDS operations first, as it is checking permissions for us.
2035  * We don't to the MDS RPC if there is nothing that we want to store there,
2036  * otherwise there is no harm in updating mtime/atime on the MDS if we are
2037  * going to do an RPC anyways.
2038  *
2039  * If we are doing a truncate, we will send the mtime and ctime updates
2040  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
2041  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
2042  * at the same time.
2043  *
2044  * In case of HSMimport, we only set attr on MDS.
2045  */
2046 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr,
2047                    enum op_xvalid xvalid, bool hsm_import)
2048 {
2049         struct inode *inode = dentry->d_inode;
2050         struct ll_inode_info *lli = ll_i2info(inode);
2051         struct md_op_data *op_data = NULL;
2052         ktime_t kstart = ktime_get();
2053         int rc = 0;
2054
2055         ENTRY;
2056
2057         CDEBUG(D_VFSTRACE, "%s: setattr inode "DFID"(%p) from %llu to %llu, "
2058                "valid %x, hsm_import %d\n",
2059                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid),
2060                inode, i_size_read(inode), attr->ia_size, attr->ia_valid,
2061                hsm_import);
2062
2063         if (attr->ia_valid & ATTR_SIZE) {
2064                 /* Check new size against VFS/VM file size limit and rlimit */
2065                 rc = inode_newsize_ok(inode, attr->ia_size);
2066                 if (rc)
2067                         RETURN(rc);
2068
2069                 /* The maximum Lustre file size is variable, based on the
2070                  * OST maximum object size and number of stripes.  This
2071                  * needs another check in addition to the VFS check above. */
2072                 if (attr->ia_size > ll_file_maxbytes(inode)) {
2073                         CDEBUG(D_INODE,"file "DFID" too large %llu > %llu\n",
2074                                PFID(&lli->lli_fid), attr->ia_size,
2075                                ll_file_maxbytes(inode));
2076                         RETURN(-EFBIG);
2077                 }
2078
2079                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
2080         }
2081
2082         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
2083         if (attr->ia_valid & TIMES_SET_FLAGS) {
2084                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
2085                     !capable(CAP_FOWNER))
2086                         RETURN(-EPERM);
2087         }
2088
2089         /* We mark all of the fields "set" so MDS/OST does not re-set them */
2090         if (!(xvalid & OP_XVALID_CTIME_SET) &&
2091              (attr->ia_valid & ATTR_CTIME)) {
2092                 attr->ia_ctime = current_time(inode);
2093                 xvalid |= OP_XVALID_CTIME_SET;
2094         }
2095         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
2096             (attr->ia_valid & ATTR_ATIME)) {
2097                 attr->ia_atime = current_time(inode);
2098                 attr->ia_valid |= ATTR_ATIME_SET;
2099         }
2100         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
2101             (attr->ia_valid & ATTR_MTIME)) {
2102                 attr->ia_mtime = current_time(inode);
2103                 attr->ia_valid |= ATTR_MTIME_SET;
2104         }
2105
2106         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2107                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld, now = %lld\n",
2108                        (s64)attr->ia_mtime.tv_sec, (s64)attr->ia_ctime.tv_sec,
2109                        ktime_get_real_seconds());
2110
2111         if (S_ISREG(inode->i_mode))
2112                 inode_unlock(inode);
2113
2114         /* We always do an MDS RPC, even if we're only changing the size;
2115          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
2116
2117         OBD_ALLOC_PTR(op_data);
2118         if (op_data == NULL)
2119                 GOTO(out, rc = -ENOMEM);
2120
2121         if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
2122                 /* If we are changing file size, file content is
2123                  * modified, flag it.
2124                  */
2125                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2126                 op_data->op_bias |= MDS_DATA_MODIFIED;
2127                 clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags);
2128         }
2129
2130         if (attr->ia_valid & ATTR_FILE) {
2131                 struct ll_file_data *fd = attr->ia_file->private_data;
2132
2133                 if (fd->fd_lease_och)
2134                         op_data->op_bias |= MDS_TRUNC_KEEP_LEASE;
2135         }
2136
2137         op_data->op_attr = *attr;
2138         op_data->op_xvalid = xvalid;
2139
2140         rc = ll_md_setattr(dentry, op_data);
2141         if (rc)
2142                 GOTO(out, rc);
2143
2144         if (!S_ISREG(inode->i_mode) || hsm_import)
2145                 GOTO(out, rc = 0);
2146
2147         if (attr->ia_valid & (ATTR_SIZE | ATTR_ATIME | ATTR_ATIME_SET |
2148                               ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME) ||
2149             xvalid & OP_XVALID_CTIME_SET) {
2150                 bool cached = false;
2151
2152                 rc = pcc_inode_setattr(inode, attr, &cached);
2153                 if (cached) {
2154                         if (rc) {
2155                                 CERROR("%s: PCC inode "DFID" setattr failed: "
2156                                        "rc = %d\n",
2157                                        ll_i2sbi(inode)->ll_fsname,
2158                                        PFID(&lli->lli_fid), rc);
2159                                 GOTO(out, rc);
2160                         }
2161                 } else {
2162                         unsigned int flags = 0;
2163
2164                         /* For truncate and utimes sending attributes to OSTs,
2165                          * setting mtime/atime to the past will be performed
2166                          * under PW [0:EOF] extent lock (new_size:EOF for
2167                          * truncate). It may seem excessive to send mtime/atime
2168                          * updates to OSTs when not setting times to past, but
2169                          * it is necessary due to possible time
2170                          * de-synchronization between MDT inode and OST objects
2171                          */
2172                         if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
2173                                 xvalid |= OP_XVALID_FLAGS;
2174                                 flags = LUSTRE_ENCRYPT_FL;
2175                                 /* Call to ll_io_zero_page is not necessary if
2176                                  * truncating on PAGE_SIZE boundary, because
2177                                  * whole pages will be wiped.
2178                                  * In case of Direct IO, all we need is to set
2179                                  * new size.
2180                                  */
2181                                 if (attr->ia_valid & ATTR_SIZE &&
2182                                     attr->ia_size & ~PAGE_MASK &&
2183                                     !(attr->ia_valid & ATTR_FILE &&
2184                                       attr->ia_file->f_flags & O_DIRECT)) {
2185                                         pgoff_t offset =
2186                                                 attr->ia_size & (PAGE_SIZE - 1);
2187
2188                                         rc = ll_io_zero_page(inode,
2189                                                     attr->ia_size >> PAGE_SHIFT,
2190                                                     offset, PAGE_SIZE - offset);
2191                                         if (rc)
2192                                                 GOTO(out, rc);
2193                                 }
2194                         }
2195                         rc = cl_setattr_ost(lli->lli_clob, attr, xvalid, flags);
2196                 }
2197         }
2198
2199         /* If the file was restored, it needs to set dirty flag.
2200          *
2201          * We've already sent MDS_DATA_MODIFIED flag in
2202          * ll_md_setattr() for truncate. However, the MDT refuses to
2203          * set the HS_DIRTY flag on released files, so we have to set
2204          * it again if the file has been restored. Please check how
2205          * LLIF_DATA_MODIFIED is set in vvp_io_setattr_fini().
2206          *
2207          * Please notice that if the file is not released, the previous
2208          * MDS_DATA_MODIFIED has taken effect and usually
2209          * LLIF_DATA_MODIFIED is not set(see vvp_io_setattr_fini()).
2210          * This way we can save an RPC for common open + trunc
2211          * operation. */
2212         if (test_and_clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags)) {
2213                 struct hsm_state_set hss = {
2214                         .hss_valid = HSS_SETMASK,
2215                         .hss_setmask = HS_DIRTY,
2216                 };
2217                 int rc2;
2218
2219                 rc2 = ll_hsm_state_set(inode, &hss);
2220                 /* truncate and write can happen at the same time, so that
2221                  * the file can be set modified even though the file is not
2222                  * restored from released state, and ll_hsm_state_set() is
2223                  * not applicable for the file, and rc2 < 0 is normal in this
2224                  * case. */
2225                 if (rc2 < 0)
2226                         CDEBUG(D_INFO, DFID "HSM set dirty failed: rc2 = %d\n",
2227                                PFID(ll_inode2fid(inode)), rc2);
2228         }
2229
2230         EXIT;
2231 out:
2232         if (op_data != NULL)
2233                 ll_finish_md_op_data(op_data);
2234
2235         if (S_ISREG(inode->i_mode)) {
2236                 inode_lock(inode);
2237                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
2238                         inode_dio_wait(inode);
2239                 /* Once we've got the i_mutex, it's safe to set the S_NOSEC
2240                  * flag.  ll_update_inode (called from ll_md_setattr), clears
2241                  * inode flags, so there is a gap where S_NOSEC is not set.
2242                  * This can cause a writer to take the i_mutex unnecessarily,
2243                  * but this is safe to do and should be rare. */
2244                 inode_has_no_xattr(inode);
2245         }
2246
2247         if (!rc)
2248                 ll_stats_ops_tally(ll_i2sbi(inode), attr->ia_valid & ATTR_SIZE ?
2249                                         LPROC_LL_TRUNC : LPROC_LL_SETATTR,
2250                                    ktime_us_delta(ktime_get(), kstart));
2251
2252         return rc;
2253 }
2254
2255 int ll_setattr(struct dentry *de, struct iattr *attr)
2256 {
2257         int mode = de->d_inode->i_mode;
2258         enum op_xvalid xvalid = 0;
2259         int rc;
2260
2261         rc = llcrypt_prepare_setattr(de, attr);
2262         if (rc)
2263                 return rc;
2264
2265         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
2266                               (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
2267                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2268
2269         if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
2270                                (ATTR_SIZE|ATTR_MODE)) &&
2271             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
2272              (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2273               !(attr->ia_mode & S_ISGID))))
2274                 attr->ia_valid |= ATTR_FORCE;
2275
2276         if ((attr->ia_valid & ATTR_MODE) &&
2277             (mode & S_ISUID) &&
2278             !(attr->ia_mode & S_ISUID) &&
2279             !(attr->ia_valid & ATTR_KILL_SUID))
2280                 attr->ia_valid |= ATTR_KILL_SUID;
2281
2282         if ((attr->ia_valid & ATTR_MODE) &&
2283             ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2284             !(attr->ia_mode & S_ISGID) &&
2285             !(attr->ia_valid & ATTR_KILL_SGID))
2286                 attr->ia_valid |= ATTR_KILL_SGID;
2287
2288         return ll_setattr_raw(de, attr, xvalid, false);
2289 }
2290
2291 int ll_statfs_internal(struct ll_sb_info *sbi, struct obd_statfs *osfs,
2292                        u32 flags)
2293 {
2294         struct obd_statfs obd_osfs = { 0 };
2295         time64_t max_age;
2296         int rc;
2297
2298         ENTRY;
2299         max_age = ktime_get_seconds() - sbi->ll_statfs_max_age;
2300
2301         if (test_bit(LL_SBI_LAZYSTATFS, sbi->ll_flags))
2302                 flags |= OBD_STATFS_NODELAY;
2303
2304         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
2305         if (rc)
2306                 RETURN(rc);
2307
2308         osfs->os_type = LL_SUPER_MAGIC;
2309
2310         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
2311               osfs->os_bavail, osfs->os_blocks, osfs->os_ffree, osfs->os_files);
2312
2313         if (osfs->os_state & OS_STATFS_SUM)
2314                 GOTO(out, rc);
2315
2316         rc = obd_statfs(NULL, sbi->ll_dt_exp, &obd_osfs, max_age, flags);
2317         if (rc) /* Possibly a filesystem with no OSTs.  Report MDT totals. */
2318                 GOTO(out, rc = 0);
2319
2320         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
2321                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
2322                obd_osfs.os_files);
2323
2324         osfs->os_bsize = obd_osfs.os_bsize;
2325         osfs->os_blocks = obd_osfs.os_blocks;
2326         osfs->os_bfree = obd_osfs.os_bfree;
2327         osfs->os_bavail = obd_osfs.os_bavail;
2328
2329         /* If we have _some_ OSTs, but don't have as many free objects on the
2330          * OSTs as inodes on the MDTs, reduce the reported number of inodes
2331          * to compensate, so that the "inodes in use" number is correct.
2332          * This should be kept in sync with lod_statfs() behaviour.
2333          */
2334         if (obd_osfs.os_files && obd_osfs.os_ffree < osfs->os_ffree) {
2335                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
2336                                  obd_osfs.os_ffree;
2337                 osfs->os_ffree = obd_osfs.os_ffree;
2338         }
2339
2340 out:
2341         RETURN(rc);
2342 }
2343
2344 static int ll_statfs_project(struct inode *inode, struct kstatfs *sfs)
2345 {
2346         struct if_quotactl qctl = {
2347                 .qc_cmd = LUSTRE_Q_GETQUOTA,
2348                 .qc_type = PRJQUOTA,
2349                 .qc_valid = QC_GENERAL,
2350         };
2351         u64 limit, curblock;
2352         int ret;
2353
2354         qctl.qc_id = ll_i2info(inode)->lli_projid;
2355         ret = quotactl_ioctl(inode->i_sb, &qctl);
2356         if (ret) {
2357                 /* ignore errors if project ID does not have
2358                  * a quota limit or feature unsupported.
2359                  */
2360                 if (ret == -ESRCH || ret == -EOPNOTSUPP)
2361                         ret = 0;
2362                 return ret;
2363         }
2364
2365         limit = ((qctl.qc_dqblk.dqb_bsoftlimit ?
2366                  qctl.qc_dqblk.dqb_bsoftlimit :
2367                  qctl.qc_dqblk.dqb_bhardlimit) * 1024) / sfs->f_bsize;
2368         if (limit && sfs->f_blocks > limit) {
2369                 curblock = (qctl.qc_dqblk.dqb_curspace +
2370                                 sfs->f_bsize - 1) / sfs->f_bsize;
2371                 sfs->f_blocks = limit;
2372                 sfs->f_bfree = sfs->f_bavail =
2373                         (sfs->f_blocks > curblock) ?
2374                         (sfs->f_blocks - curblock) : 0;
2375         }
2376
2377         limit = qctl.qc_dqblk.dqb_isoftlimit ?
2378                 qctl.qc_dqblk.dqb_isoftlimit :
2379                 qctl.qc_dqblk.dqb_ihardlimit;
2380         if (limit && sfs->f_files > limit) {
2381                 sfs->f_files = limit;
2382                 sfs->f_ffree = (sfs->f_files >
2383                         qctl.qc_dqblk.dqb_curinodes) ?
2384                         (sfs->f_files - qctl.qc_dqblk.dqb_curinodes) : 0;
2385         }
2386
2387         return 0;
2388 }
2389
2390 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
2391 {
2392         struct super_block *sb = de->d_sb;
2393         struct obd_statfs osfs;
2394         __u64 fsid = huge_encode_dev(sb->s_dev);
2395         ktime_t kstart = ktime_get();
2396         int rc;
2397
2398         CDEBUG(D_VFSTRACE, "VFS Op:sb=%s (%p)\n", sb->s_id, sb);
2399
2400         /* Some amount of caching on the client is allowed */
2401         rc = ll_statfs_internal(ll_s2sbi(sb), &osfs, OBD_STATFS_SUM);
2402         if (rc)
2403                 return rc;
2404
2405         statfs_unpack(sfs, &osfs);
2406
2407         /* We need to downshift for all 32-bit kernels, because we can't
2408          * tell if the kernel is being called via sys_statfs64() or not.
2409          * Stop before overflowing f_bsize - in which case it is better
2410          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
2411         if (sizeof(long) < 8) {
2412                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
2413                         sfs->f_bsize <<= 1;
2414
2415                         osfs.os_blocks >>= 1;
2416                         osfs.os_bfree >>= 1;
2417                         osfs.os_bavail >>= 1;
2418                 }
2419         }
2420
2421         sfs->f_blocks = osfs.os_blocks;
2422         sfs->f_bfree = osfs.os_bfree;
2423         sfs->f_bavail = osfs.os_bavail;
2424         sfs->f_fsid.val[0] = (__u32)fsid;
2425         sfs->f_fsid.val[1] = (__u32)(fsid >> 32);
2426         if (ll_i2info(de->d_inode)->lli_projid)
2427                 return ll_statfs_project(de->d_inode, sfs);
2428
2429         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STATFS,
2430                            ktime_us_delta(ktime_get(), kstart));
2431
2432         return 0;
2433 }
2434
2435 void ll_inode_size_lock(struct inode *inode)
2436 {
2437         struct ll_inode_info *lli;
2438
2439         LASSERT(!S_ISDIR(inode->i_mode));
2440
2441         lli = ll_i2info(inode);
2442         mutex_lock(&lli->lli_size_mutex);
2443 }
2444
2445 void ll_inode_size_unlock(struct inode *inode)
2446 {
2447         struct ll_inode_info *lli;
2448
2449         lli = ll_i2info(inode);
2450         mutex_unlock(&lli->lli_size_mutex);
2451 }
2452
2453 void ll_update_inode_flags(struct inode *inode, unsigned int ext_flags)
2454 {
2455         /* do not clear encryption flag */
2456         ext_flags |= ll_inode_to_ext_flags(inode->i_flags) & LUSTRE_ENCRYPT_FL;
2457         inode->i_flags = ll_ext_to_inode_flags(ext_flags);
2458         if (ext_flags & LUSTRE_PROJINHERIT_FL)
2459                 set_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2460         else
2461                 clear_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2462 }
2463
2464 int ll_update_inode(struct inode *inode, struct lustre_md *md)
2465 {
2466         struct ll_inode_info *lli = ll_i2info(inode);
2467         struct mdt_body *body = md->body;
2468         struct ll_sb_info *sbi = ll_i2sbi(inode);
2469         bool api32;
2470         int rc = 0;
2471
2472         if (body->mbo_valid & OBD_MD_FLEASIZE) {
2473                 rc = cl_file_inode_init(inode, md);
2474                 if (rc)
2475                         return rc;
2476         }
2477
2478         if (S_ISDIR(inode->i_mode)) {
2479                 rc = ll_update_lsm_md(inode, md);
2480                 if (rc != 0)
2481                         return rc;
2482         }
2483
2484         if (body->mbo_valid & OBD_MD_FLACL)
2485                 lli_replace_acl(lli, md);
2486
2487         api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
2488         inode->i_ino = cl_fid_build_ino(&body->mbo_fid1, api32);
2489         inode->i_generation = cl_fid_build_gen(&body->mbo_fid1);
2490
2491         if (body->mbo_valid & OBD_MD_FLATIME) {
2492                 if (body->mbo_atime > inode->i_atime.tv_sec)
2493                         inode->i_atime.tv_sec = body->mbo_atime;
2494                 lli->lli_atime = body->mbo_atime;
2495         }
2496
2497         if (body->mbo_valid & OBD_MD_FLMTIME) {
2498                 if (body->mbo_mtime > inode->i_mtime.tv_sec) {
2499                         CDEBUG(D_INODE,
2500                                "setting ino %lu mtime from %lld to %llu\n",
2501                                inode->i_ino, (s64)inode->i_mtime.tv_sec,
2502                                body->mbo_mtime);
2503                         inode->i_mtime.tv_sec = body->mbo_mtime;
2504                 }
2505                 lli->lli_mtime = body->mbo_mtime;
2506         }
2507
2508         if (body->mbo_valid & OBD_MD_FLCTIME) {
2509                 if (body->mbo_ctime > inode->i_ctime.tv_sec)
2510                         inode->i_ctime.tv_sec = body->mbo_ctime;
2511                 lli->lli_ctime = body->mbo_ctime;
2512         }
2513
2514         if (body->mbo_valid & OBD_MD_FLBTIME)
2515                 lli->lli_btime = body->mbo_btime;
2516
2517         /* Clear i_flags to remove S_NOSEC before permissions are updated */
2518         if (body->mbo_valid & OBD_MD_FLFLAGS)
2519                 ll_update_inode_flags(inode, body->mbo_flags);
2520         if (body->mbo_valid & OBD_MD_FLMODE)
2521                 inode->i_mode = (inode->i_mode & S_IFMT) |
2522                                 (body->mbo_mode & ~S_IFMT);
2523
2524         if (body->mbo_valid & OBD_MD_FLTYPE)
2525                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
2526                                 (body->mbo_mode & S_IFMT);
2527
2528         LASSERT(inode->i_mode != 0);
2529         if (body->mbo_valid & OBD_MD_FLUID)
2530                 inode->i_uid = make_kuid(&init_user_ns, body->mbo_uid);
2531         if (body->mbo_valid & OBD_MD_FLGID)
2532                 inode->i_gid = make_kgid(&init_user_ns, body->mbo_gid);
2533         if (body->mbo_valid & OBD_MD_FLPROJID)
2534                 lli->lli_projid = body->mbo_projid;
2535         if (body->mbo_valid & OBD_MD_FLNLINK)
2536                 set_nlink(inode, body->mbo_nlink);
2537         if (body->mbo_valid & OBD_MD_FLRDEV)
2538                 inode->i_rdev = old_decode_dev(body->mbo_rdev);
2539
2540         if (body->mbo_valid & OBD_MD_FLID) {
2541                 /* FID shouldn't be changed! */
2542                 if (fid_is_sane(&lli->lli_fid)) {
2543                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->mbo_fid1),
2544                                  "Trying to change FID "DFID
2545                                  " to the "DFID", inode "DFID"(%p)\n",
2546                                  PFID(&lli->lli_fid), PFID(&body->mbo_fid1),
2547                                  PFID(ll_inode2fid(inode)), inode);
2548                 } else {
2549                         lli->lli_fid = body->mbo_fid1;
2550                 }
2551         }
2552
2553         LASSERT(fid_seq(&lli->lli_fid) != 0);
2554
2555         lli->lli_attr_valid = body->mbo_valid;
2556         if (body->mbo_valid & OBD_MD_FLSIZE) {
2557                 i_size_write(inode, body->mbo_size);
2558
2559                 CDEBUG(D_VFSTRACE, "inode="DFID", updating i_size %llu\n",
2560                        PFID(ll_inode2fid(inode)),
2561                        (unsigned long long)body->mbo_size);
2562
2563                 if (body->mbo_valid & OBD_MD_FLBLOCKS)
2564                         inode->i_blocks = body->mbo_blocks;
2565         } else {
2566                 if (body->mbo_valid & OBD_MD_FLLAZYSIZE)
2567                         lli->lli_lazysize = body->mbo_size;
2568                 if (body->mbo_valid & OBD_MD_FLLAZYBLOCKS)
2569                         lli->lli_lazyblocks = body->mbo_blocks;
2570         }
2571
2572         if (body->mbo_valid & OBD_MD_TSTATE) {
2573                 /* Set LLIF_FILE_RESTORING if restore ongoing and
2574                  * clear it when done to ensure to start again
2575                  * glimpsing updated attrs
2576                  */
2577                 if (body->mbo_t_state & MS_RESTORE)
2578                         set_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2579                 else
2580                         clear_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2581         }
2582
2583         return 0;
2584 }
2585
2586 /* update directory depth to ROOT, called after LOOKUP lock is fetched. */
2587 void ll_update_dir_depth(struct inode *dir, struct inode *inode)
2588 {
2589         struct ll_inode_info *lli;
2590
2591         if (!S_ISDIR(inode->i_mode))
2592                 return;
2593
2594         if (inode == dir)
2595                 return;
2596
2597         lli = ll_i2info(inode);
2598         lli->lli_depth = ll_i2info(dir)->lli_depth + 1;
2599         CDEBUG(D_INODE, DFID" depth %hu\n", PFID(&lli->lli_fid), lli->lli_depth);
2600 }
2601
2602 void ll_truncate_inode_pages_final(struct inode *inode)
2603 {
2604         struct address_space *mapping = &inode->i_data;
2605         unsigned long nrpages;
2606         unsigned long flags;
2607
2608         truncate_inode_pages_final(mapping);
2609
2610         /* Workaround for LU-118: Note nrpages may not be totally updated when
2611          * truncate_inode_pages() returns, as there can be a page in the process
2612          * of deletion (inside __delete_from_page_cache()) in the specified
2613          * range. Thus mapping->nrpages can be non-zero when this function
2614          * returns even after truncation of the whole mapping.  Only do this if
2615          * npages isn't already zero.
2616          */
2617         nrpages = mapping->nrpages;
2618         if (nrpages) {
2619                 ll_xa_lock_irqsave(&mapping->i_pages, flags);
2620                 nrpages = mapping->nrpages;
2621                 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
2622         } /* Workaround end */
2623
2624         LASSERTF(nrpages == 0, "%s: inode="DFID"(%p) nrpages=%lu, "
2625                  "see https://jira.whamcloud.com/browse/LU-118\n",
2626                  ll_i2sbi(inode)->ll_fsname,
2627                  PFID(ll_inode2fid(inode)), inode, nrpages);
2628 }
2629
2630 int ll_read_inode2(struct inode *inode, void *opaque)
2631 {
2632         struct lustre_md *md = opaque;
2633         struct ll_inode_info *lli = ll_i2info(inode);
2634         int     rc;
2635         ENTRY;
2636
2637         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
2638                PFID(&lli->lli_fid), inode);
2639
2640         /* Core attributes from the MDS first.  This is a new inode, and
2641          * the VFS doesn't zero times in the core inode so we have to do
2642          * it ourselves.  They will be overwritten by either MDS or OST
2643          * attributes - we just need to make sure they aren't newer.
2644          */
2645         inode->i_mtime.tv_sec = 0;
2646         inode->i_atime.tv_sec = 0;
2647         inode->i_ctime.tv_sec = 0;
2648         inode->i_rdev = 0;
2649         rc = ll_update_inode(inode, md);
2650         if (rc != 0)
2651                 RETURN(rc);
2652
2653         /* OIDEBUG(inode); */
2654
2655 #ifdef HAVE_BACKING_DEV_INFO
2656         /* initializing backing dev info. */
2657         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
2658 #endif
2659         if (S_ISREG(inode->i_mode)) {
2660                 struct ll_sb_info *sbi = ll_i2sbi(inode);
2661                 inode->i_op = &ll_file_inode_operations;
2662                 inode->i_fop = sbi->ll_fop;
2663                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
2664                 EXIT;
2665         } else if (S_ISDIR(inode->i_mode)) {
2666                 inode->i_op = &ll_dir_inode_operations;
2667                 inode->i_fop = &ll_dir_operations;
2668                 EXIT;
2669         } else if (S_ISLNK(inode->i_mode)) {
2670                 inode->i_op = &ll_fast_symlink_inode_operations;
2671                 EXIT;
2672         } else {
2673                 inode->i_op = &ll_special_inode_operations;
2674
2675                 init_special_inode(inode, inode->i_mode,
2676                                    inode->i_rdev);
2677
2678                 EXIT;
2679         }
2680
2681         return 0;
2682 }
2683
2684 void ll_delete_inode(struct inode *inode)
2685 {
2686         struct ll_inode_info *lli = ll_i2info(inode);
2687         ENTRY;
2688
2689         if (S_ISREG(inode->i_mode) && lli->lli_clob != NULL) {
2690                 /* It is last chance to write out dirty pages,
2691                  * otherwise we may lose data while umount.
2692                  *
2693                  * If i_nlink is 0 then just discard data. This is safe because
2694                  * local inode gets i_nlink 0 from server only for the last
2695                  * unlink, so that file is not opened somewhere else
2696                  */
2697                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, inode->i_nlink ?
2698                                    CL_FSYNC_LOCAL : CL_FSYNC_DISCARD, 1);
2699         }
2700
2701         ll_truncate_inode_pages_final(inode);
2702         ll_clear_inode(inode);
2703         clear_inode(inode);
2704
2705         EXIT;
2706 }
2707
2708 int ll_iocontrol(struct inode *inode, struct file *file,
2709                  unsigned int cmd, unsigned long arg)
2710 {
2711         struct ll_sb_info *sbi = ll_i2sbi(inode);
2712         struct ptlrpc_request *req = NULL;
2713         int rc, flags = 0;
2714         ENTRY;
2715
2716         switch (cmd) {
2717         case FS_IOC_GETFLAGS: {
2718                 struct mdt_body *body;
2719                 struct md_op_data *op_data;
2720
2721                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2722                                              0, 0, LUSTRE_OPC_ANY,
2723                                              NULL);
2724                 if (IS_ERR(op_data))
2725                         RETURN(PTR_ERR(op_data));
2726
2727                 op_data->op_valid = OBD_MD_FLFLAGS;
2728                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2729                 ll_finish_md_op_data(op_data);
2730                 if (rc) {
2731                         CERROR("%s: failure inode "DFID": rc = %d\n",
2732                                sbi->ll_md_exp->exp_obd->obd_name,
2733                                PFID(ll_inode2fid(inode)), rc);
2734                         RETURN(-abs(rc));
2735                 }
2736
2737                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2738
2739                 flags = body->mbo_flags;
2740
2741                 ptlrpc_req_finished(req);
2742
2743                 RETURN(put_user(flags, (int __user *)arg));
2744         }
2745         case FS_IOC_SETFLAGS: {
2746                 struct iattr *attr;
2747                 struct md_op_data *op_data;
2748                 struct cl_object *obj;
2749                 struct fsxattr fa = { 0 };
2750
2751                 if (get_user(flags, (int __user *)arg))
2752                         RETURN(-EFAULT);
2753
2754                 fa.fsx_projid = ll_i2info(inode)->lli_projid;
2755                 if (flags & LUSTRE_PROJINHERIT_FL)
2756                         fa.fsx_xflags = FS_XFLAG_PROJINHERIT;
2757
2758                 rc = ll_ioctl_check_project(inode, fa.fsx_xflags,
2759                                             fa.fsx_projid);
2760                 if (rc)
2761                         RETURN(rc);
2762
2763                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2764                                              LUSTRE_OPC_ANY, NULL);
2765                 if (IS_ERR(op_data))
2766                         RETURN(PTR_ERR(op_data));
2767
2768                 op_data->op_attr_flags = flags;
2769                 op_data->op_xvalid |= OP_XVALID_FLAGS;
2770                 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &req);
2771                 ll_finish_md_op_data(op_data);
2772                 ptlrpc_req_finished(req);
2773                 if (rc)
2774                         RETURN(rc);
2775
2776                 ll_update_inode_flags(inode, flags);
2777
2778                 obj = ll_i2info(inode)->lli_clob;
2779                 if (obj == NULL)
2780                         RETURN(0);
2781
2782                 OBD_ALLOC_PTR(attr);
2783                 if (attr == NULL)
2784                         RETURN(-ENOMEM);
2785
2786                 rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS, flags);
2787
2788                 OBD_FREE_PTR(attr);
2789                 RETURN(rc);
2790         }
2791         default:
2792                 RETURN(-ENOSYS);
2793         }
2794
2795         RETURN(0);
2796 }
2797
2798 int ll_flush_ctx(struct inode *inode)
2799 {
2800         struct ll_sb_info  *sbi = ll_i2sbi(inode);
2801
2802         CDEBUG(D_SEC, "flush context for user %d\n",
2803                from_kuid(&init_user_ns, current_uid()));
2804
2805         obd_set_info_async(NULL, sbi->ll_md_exp,
2806                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2807                            0, NULL, NULL);
2808         obd_set_info_async(NULL, sbi->ll_dt_exp,
2809                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2810                            0, NULL, NULL);
2811         return 0;
2812 }
2813
2814 /* umount -f client means force down, don't save state */
2815 void ll_umount_begin(struct super_block *sb)
2816 {
2817         struct ll_sb_info *sbi = ll_s2sbi(sb);
2818         struct obd_device *obd;
2819         struct obd_ioctl_data *ioc_data;
2820         int cnt;
2821         ENTRY;
2822
2823         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2824                sb->s_count, atomic_read(&sb->s_active));
2825
2826         obd = class_exp2obd(sbi->ll_md_exp);
2827         if (obd == NULL) {
2828                 CERROR("Invalid MDC connection handle %#llx\n",
2829                        sbi->ll_md_exp->exp_handle.h_cookie);
2830                 EXIT;
2831                 return;
2832         }
2833         obd->obd_force = 1;
2834
2835         obd = class_exp2obd(sbi->ll_dt_exp);
2836         if (obd == NULL) {
2837                 CERROR("Invalid LOV connection handle %#llx\n",
2838                        sbi->ll_dt_exp->exp_handle.h_cookie);
2839                 EXIT;
2840                 return;
2841         }
2842         obd->obd_force = 1;
2843
2844         OBD_ALLOC_PTR(ioc_data);
2845         if (ioc_data) {
2846                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2847                               sizeof *ioc_data, ioc_data, NULL);
2848
2849                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2850                               sizeof *ioc_data, ioc_data, NULL);
2851
2852                 OBD_FREE_PTR(ioc_data);
2853         }
2854
2855         /* Really, we'd like to wait until there are no requests outstanding,
2856          * and then continue.  For now, we just periodically checking for vfs
2857          * to decrement mnt_cnt and hope to finish it within 10sec.
2858          */
2859         cnt = 10;
2860         while (cnt > 0 &&
2861                !may_umount(sbi->ll_mnt.mnt)) {
2862                 ssleep(1);
2863                 cnt -= 1;
2864         }
2865
2866         EXIT;
2867 }
2868
2869 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2870 {
2871         struct ll_sb_info *sbi = ll_s2sbi(sb);
2872         char *profilenm = get_profile_name(sb);
2873         int err;
2874         __u32 read_only;
2875
2876         if ((*flags & MS_RDONLY) != (sb->s_flags & SB_RDONLY)) {
2877                 read_only = *flags & MS_RDONLY;
2878                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
2879                                          sizeof(KEY_READ_ONLY),
2880                                          KEY_READ_ONLY, sizeof(read_only),
2881                                          &read_only, NULL);
2882                 if (err) {
2883                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
2884                                       profilenm, read_only ?
2885                                       "read-only" : "read-write", err);
2886                         return err;
2887                 }
2888
2889                 if (read_only)
2890                         sb->s_flags |= SB_RDONLY;
2891                 else
2892                         sb->s_flags &= ~SB_RDONLY;
2893
2894                 if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
2895                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
2896                                       read_only ?  "read-only" : "read-write");
2897         }
2898         return 0;
2899 }
2900
2901 /**
2902  * Cleanup the open handle that is cached on MDT-side.
2903  *
2904  * For open case, the client side open handling thread may hit error
2905  * after the MDT grant the open. Under such case, the client should
2906  * send close RPC to the MDT as cleanup; otherwise, the open handle
2907  * on the MDT will be leaked there until the client umount or evicted.
2908  *
2909  * In further, if someone unlinked the file, because the open handle
2910  * holds the reference on such file/object, then it will block the
2911  * subsequent threads that want to locate such object via FID.
2912  *
2913  * \param[in] sb        super block for this file-system
2914  * \param[in] open_req  pointer to the original open request
2915  */
2916 void ll_open_cleanup(struct super_block *sb, struct req_capsule *pill)
2917 {
2918         struct mdt_body                 *body;
2919         struct md_op_data               *op_data;
2920         struct ptlrpc_request           *close_req = NULL;
2921         struct obd_export               *exp       = ll_s2sbi(sb)->ll_md_exp;
2922         ENTRY;
2923
2924         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
2925         OBD_ALLOC_PTR(op_data);
2926         if (op_data == NULL) {
2927                 CWARN("%s: cannot allocate op_data to release open handle for "
2928                       DFID"\n", ll_s2sbi(sb)->ll_fsname, PFID(&body->mbo_fid1));
2929
2930                 RETURN_EXIT;
2931         }
2932
2933         op_data->op_fid1 = body->mbo_fid1;
2934         op_data->op_open_handle = body->mbo_open_handle;
2935         op_data->op_mod_time = ktime_get_real_seconds();
2936         md_close(exp, op_data, NULL, &close_req);
2937         ptlrpc_req_finished(close_req);
2938         ll_finish_md_op_data(op_data);
2939
2940         EXIT;
2941 }
2942
2943 int ll_prep_inode(struct inode **inode, struct req_capsule *pill,
2944                   struct super_block *sb, struct lookup_intent *it)
2945 {
2946         struct ll_sb_info *sbi = NULL;
2947         struct lustre_md md = { NULL };
2948         bool default_lmv_deleted = false;
2949         int rc;
2950
2951         ENTRY;
2952
2953         LASSERT(*inode || sb);
2954         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2955         rc = md_get_lustre_md(sbi->ll_md_exp, pill, sbi->ll_dt_exp,
2956                               sbi->ll_md_exp, &md);
2957         if (rc != 0)
2958                 GOTO(out, rc);
2959
2960         /*
2961          * clear default_lmv only if intent_getattr reply doesn't contain it.
2962          * but it needs to be done after iget, check this early because
2963          * ll_update_lsm_md() may change md.
2964          */
2965         if (it && (it->it_op & (IT_LOOKUP | IT_GETATTR)) &&
2966             S_ISDIR(md.body->mbo_mode) && !md.default_lmv)
2967                 default_lmv_deleted = true;
2968
2969         if (*inode) {
2970                 rc = ll_update_inode(*inode, &md);
2971                 if (rc != 0)
2972                         GOTO(out, rc);
2973         } else {
2974                 bool api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
2975                 struct lu_fid *fid1 = &md.body->mbo_fid1;
2976
2977                 LASSERT(sb != NULL);
2978
2979                 /*
2980                  * At this point server returns to client's same fid as client
2981                  * generated for creating. So using ->fid1 is okay here.
2982                  */
2983                 if (!fid_is_sane(fid1)) {
2984                         CERROR("%s: Fid is insane "DFID"\n",
2985                                 sbi->ll_fsname, PFID(fid1));
2986                         GOTO(out, rc = -EINVAL);
2987                 }
2988
2989                 *inode = ll_iget(sb, cl_fid_build_ino(fid1, api32), &md);
2990                 if (IS_ERR(*inode)) {
2991                         lmd_clear_acl(&md);
2992                         rc = IS_ERR(*inode) ? PTR_ERR(*inode) : -ENOMEM;
2993                         *inode = NULL;
2994                         CERROR("new_inode -fatal: rc %d\n", rc);
2995                         GOTO(out, rc);
2996                 }
2997         }
2998
2999         /* Handling piggyback layout lock.
3000          * Layout lock can be piggybacked by getattr and open request.
3001          * The lsm can be applied to inode only if it comes with a layout lock
3002          * otherwise correct layout may be overwritten, for example:
3003          * 1. proc1: mdt returns a lsm but not granting layout
3004          * 2. layout was changed by another client
3005          * 3. proc2: refresh layout and layout lock granted
3006          * 4. proc1: to apply a stale layout */
3007         if (it != NULL && it->it_lock_mode != 0) {
3008                 struct lustre_handle lockh;
3009                 struct ldlm_lock *lock;
3010
3011                 lockh.cookie = it->it_lock_handle;
3012                 lock = ldlm_handle2lock(&lockh);
3013                 LASSERT(lock != NULL);
3014                 if (ldlm_has_layout(lock)) {
3015                         struct cl_object_conf conf;
3016
3017                         memset(&conf, 0, sizeof(conf));
3018                         conf.coc_opc = OBJECT_CONF_SET;
3019                         conf.coc_inode = *inode;
3020                         conf.coc_lock = lock;
3021                         conf.u.coc_layout = md.layout;
3022                         (void)ll_layout_conf(*inode, &conf);
3023                 }
3024                 LDLM_LOCK_PUT(lock);
3025         }
3026
3027         if (default_lmv_deleted)
3028                 ll_update_default_lsm_md(*inode, &md);
3029
3030         /* we may want to apply some policy for foreign file/dir */
3031         if (ll_sbi_has_foreign_symlink(sbi)) {
3032                 rc = ll_manage_foreign(*inode, &md);
3033                 if (rc < 0)
3034                         GOTO(out, rc);
3035         }
3036
3037         GOTO(out, rc = 0);
3038
3039 out:
3040         /* cleanup will be done if necessary */
3041         md_free_lustre_md(sbi->ll_md_exp, &md);
3042
3043         if (rc != 0 && it != NULL && it->it_op & IT_OPEN) {
3044                 ll_intent_drop_lock(it);
3045                 ll_open_cleanup(sb != NULL ? sb : (*inode)->i_sb, pill);
3046         }
3047
3048         return rc;
3049 }
3050
3051 int ll_obd_statfs(struct inode *inode, void __user *arg)
3052 {
3053         struct ll_sb_info *sbi = NULL;
3054         struct obd_export *exp;
3055         struct obd_ioctl_data *data = NULL;
3056         __u32 type;
3057         int len = 0, rc;
3058
3059         if (inode)
3060                 sbi = ll_i2sbi(inode);
3061         if (!sbi)
3062                 GOTO(out_statfs, rc = -EINVAL);
3063
3064         rc = obd_ioctl_getdata(&data, &len, arg);
3065         if (rc)
3066                 GOTO(out_statfs, rc);
3067
3068         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
3069             !data->ioc_pbuf1 || !data->ioc_pbuf2)
3070                 GOTO(out_statfs, rc = -EINVAL);
3071
3072         if (data->ioc_inllen1 != sizeof(__u32) ||
3073             data->ioc_inllen2 != sizeof(__u32) ||
3074             data->ioc_plen1 != sizeof(struct obd_statfs) ||
3075             data->ioc_plen2 != sizeof(struct obd_uuid))
3076                 GOTO(out_statfs, rc = -EINVAL);
3077
3078         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
3079         if (type & LL_STATFS_LMV)
3080                 exp = sbi->ll_md_exp;
3081         else if (type & LL_STATFS_LOV)
3082                 exp = sbi->ll_dt_exp;
3083         else
3084                 GOTO(out_statfs, rc = -ENODEV);
3085
3086         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, data, NULL);
3087         if (rc)
3088                 GOTO(out_statfs, rc);
3089 out_statfs:
3090         OBD_FREE_LARGE(data, len);
3091         return rc;
3092 }
3093
3094 /*
3095  * this is normally called in ll_fini_md_op_data(), but sometimes it needs to
3096  * be called early to avoid deadlock.
3097  */
3098 void ll_unlock_md_op_lsm(struct md_op_data *op_data)
3099 {
3100         if (op_data->op_mea2_sem) {
3101                 up_read_non_owner(op_data->op_mea2_sem);
3102                 op_data->op_mea2_sem = NULL;
3103         }
3104
3105         if (op_data->op_mea1_sem) {
3106                 up_read_non_owner(op_data->op_mea1_sem);
3107                 op_data->op_mea1_sem = NULL;
3108         }
3109 }
3110
3111 /* this function prepares md_op_data hint for passing it down to MD stack. */
3112 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
3113                                       struct inode *i1, struct inode *i2,
3114                                       const char *name, size_t namelen,
3115                                       __u32 mode, enum md_op_code opc,
3116                                       void *data)
3117 {
3118         struct llcrypt_name fname = { 0 };
3119         int rc;
3120
3121         LASSERT(i1 != NULL);
3122
3123         if (name == NULL) {
3124                 /* Do not reuse namelen for something else. */
3125                 if (namelen != 0)
3126                         return ERR_PTR(-EINVAL);
3127         } else {
3128                 if (namelen > ll_i2sbi(i1)->ll_namelen)
3129                         return ERR_PTR(-ENAMETOOLONG);
3130
3131                 /* "/" is not valid name, but it's allowed */
3132                 if (!lu_name_is_valid_2(name, namelen) &&
3133                     strncmp("/", name, namelen) != 0)
3134                         return ERR_PTR(-EINVAL);
3135         }
3136
3137         if (op_data == NULL)
3138                 OBD_ALLOC_PTR(op_data);
3139
3140         if (op_data == NULL)
3141                 return ERR_PTR(-ENOMEM);
3142
3143         ll_i2gids(op_data->op_suppgids, i1, i2);
3144         op_data->op_fid1 = *ll_inode2fid(i1);
3145
3146         if (S_ISDIR(i1->i_mode)) {
3147                 down_read_non_owner(&ll_i2info(i1)->lli_lsm_sem);
3148                 op_data->op_mea1_sem = &ll_i2info(i1)->lli_lsm_sem;
3149                 op_data->op_mea1 = ll_i2info(i1)->lli_lsm_md;
3150                 op_data->op_default_mea1 = ll_i2info(i1)->lli_default_lsm_md;
3151         }
3152
3153         if (i2) {
3154                 op_data->op_fid2 = *ll_inode2fid(i2);
3155                 if (S_ISDIR(i2->i_mode)) {
3156                         if (i2 != i1) {
3157                                 /* i2 is typically a child of i1, and MUST be
3158                                  * further from the root to avoid deadlocks.
3159                                  */
3160                                 down_read_non_owner(&ll_i2info(i2)->lli_lsm_sem);
3161                                 op_data->op_mea2_sem =
3162                                                 &ll_i2info(i2)->lli_lsm_sem;
3163                         }
3164                         op_data->op_mea2 = ll_i2info(i2)->lli_lsm_md;
3165                 }
3166         } else {
3167                 fid_zero(&op_data->op_fid2);
3168         }
3169
3170         if (test_bit(LL_SBI_64BIT_HASH, ll_i2sbi(i1)->ll_flags))
3171                 op_data->op_cli_flags |= CLI_HASH64;
3172
3173         if (ll_need_32bit_api(ll_i2sbi(i1)))
3174                 op_data->op_cli_flags |= CLI_API32;
3175
3176         if (opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_CREATE) {
3177                 /* In case of lookup, ll_setup_filename() has already been
3178                  * called in ll_lookup_it(), so just take provided name.
3179                  */
3180                 fname.disk_name.name = (unsigned char *)name;
3181                 fname.disk_name.len = namelen;
3182         } else if (name && namelen) {
3183                 struct qstr dname = QSTR_INIT(name, namelen);
3184                 struct inode *dir;
3185                 struct lu_fid *pfid = NULL;
3186                 struct lu_fid fid;
3187                 int lookup;
3188
3189                 if (!S_ISDIR(i1->i_mode) && i2 && S_ISDIR(i2->i_mode)) {
3190                         /* special case when called from ll_link() */
3191                         dir = i2;
3192                         lookup = 0;
3193                 } else {
3194                         dir = i1;
3195                         lookup = (int)(opc == LUSTRE_OPC_ANY);
3196                 }
3197                 if (opc == LUSTRE_OPC_ANY && lookup)
3198                         pfid = &fid;
3199                 rc = ll_setup_filename(dir, &dname, lookup, &fname, pfid);
3200                 if (rc) {
3201                         ll_finish_md_op_data(op_data);
3202                         return ERR_PTR(rc);
3203                 }
3204                 if (pfid && !fid_is_zero(pfid)) {
3205                         if (i2 == NULL)
3206                                 op_data->op_fid2 = fid;
3207                         op_data->op_bias = MDS_FID_OP;
3208                 }
3209                 if (fname.disk_name.name &&
3210                     fname.disk_name.name != (unsigned char *)name)
3211                         /* op_data->op_name must be freed after use */
3212                         op_data->op_flags |= MF_OPNAME_KMALLOCED;
3213         }
3214
3215         /* In fact LUSTRE_OPC_LOOKUP, LUSTRE_OPC_OPEN, LUSTRE_OPC_MIGR
3216          * are LUSTRE_OPC_ANY
3217          */
3218         if (opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_OPEN ||
3219             opc == LUSTRE_OPC_MIGR)
3220                 op_data->op_code = LUSTRE_OPC_ANY;
3221         else
3222                 op_data->op_code = opc;
3223         op_data->op_name = fname.disk_name.name;
3224         op_data->op_namelen = fname.disk_name.len;
3225         op_data->op_mode = mode;
3226         op_data->op_mod_time = ktime_get_real_seconds();
3227         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
3228         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
3229         op_data->op_cap = current_cap();
3230         op_data->op_mds = 0;
3231         if ((opc == LUSTRE_OPC_CREATE) && (name != NULL) &&
3232              filename_is_volatile(name, namelen, &op_data->op_mds)) {
3233                 op_data->op_bias |= MDS_CREATE_VOLATILE;
3234         }
3235         op_data->op_data = data;
3236
3237         return op_data;
3238 }
3239
3240 void ll_finish_md_op_data(struct md_op_data *op_data)
3241 {
3242         ll_unlock_md_op_lsm(op_data);
3243         ll_security_release_secctx(op_data->op_file_secctx,
3244                                    op_data->op_file_secctx_size);
3245         if (op_data->op_flags & MF_OPNAME_KMALLOCED)
3246                 /* allocated via ll_setup_filename called
3247                  * from ll_prep_md_op_data
3248                  */
3249                 kfree(op_data->op_name);
3250         llcrypt_free_ctx(op_data->op_file_encctx, op_data->op_file_encctx_size);
3251         OBD_FREE_PTR(op_data);
3252 }
3253
3254 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
3255 {
3256         struct ll_sb_info *sbi;
3257         int i;
3258
3259         LASSERT(seq && dentry);
3260         sbi = ll_s2sbi(dentry->d_sb);
3261
3262         if (test_bit(LL_SBI_NOLCK, sbi->ll_flags))
3263                 seq_puts(seq, "nolock");
3264
3265         for (i = 1; ll_sbi_flags_name[i].token != LL_SBI_NUM_MOUNT_OPT; i++) {
3266                 /* match_table in some cases has patterns for both enabled and
3267                  * disabled cases. Ignore 'no'xxx versions if bit is set.
3268                  */
3269                 if (test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3270                     strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3271                         if (ll_sbi_flags_name[i].token ==
3272                             LL_SBI_FOREIGN_SYMLINK) {
3273                                 seq_show_option(seq, "foreign_symlink",
3274                                                 sbi->ll_foreign_symlink_prefix);
3275                         } else {
3276                                 seq_printf(seq, ",%s",
3277                                            ll_sbi_flags_name[i].pattern);
3278                         }
3279
3280                         /* You can have either localflock or flock but not
3281                          * both. If localflock is set don't print flock or
3282                          * noflock.
3283                          */
3284                         if (ll_sbi_flags_name[i].token == LL_SBI_LOCALFLOCK)
3285                                 i += 2;
3286                 } else if (!test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3287                            !strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3288                         seq_printf(seq, ",%s",
3289                                    ll_sbi_flags_name[i].pattern);
3290                 }
3291         }
3292
3293         RETURN(0);
3294 }
3295
3296 /**
3297  * Get obd name by cmd, and copy out to user space
3298  */
3299 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg)
3300 {
3301         struct ll_sb_info *sbi = ll_i2sbi(inode);
3302         struct obd_device *obd;
3303         ENTRY;
3304
3305         if (cmd == OBD_IOC_GETNAME_OLD || cmd == OBD_IOC_GETDTNAME)
3306                 obd = class_exp2obd(sbi->ll_dt_exp);
3307         else if (cmd == OBD_IOC_GETMDNAME)
3308                 obd = class_exp2obd(sbi->ll_md_exp);
3309         else
3310                 RETURN(-EINVAL);
3311
3312         if (!obd)
3313                 RETURN(-ENOENT);
3314
3315         if (copy_to_user((void __user *)arg, obd->obd_name,
3316                          strlen(obd->obd_name) + 1))
3317                 RETURN(-EFAULT);
3318
3319         RETURN(0);
3320 }
3321
3322 static char* ll_d_path(struct dentry *dentry, char *buf, int bufsize)
3323 {
3324         char *path = NULL;
3325
3326         struct path p;
3327
3328         p.dentry = dentry;
3329         p.mnt = current->fs->root.mnt;
3330         path_get(&p);
3331         path = d_path(&p, buf, bufsize);
3332         path_put(&p);
3333         return path;
3334 }
3335
3336 void ll_dirty_page_discard_warn(struct page *page, int ioret)
3337 {
3338         char *buf, *path = NULL;
3339         struct dentry *dentry = NULL;
3340         struct inode *inode = page->mapping->host;
3341
3342         /* this can be called inside spin lock so use GFP_ATOMIC. */
3343         buf = (char *)__get_free_page(GFP_ATOMIC);
3344         if (buf != NULL) {
3345                 dentry = d_find_alias(page->mapping->host);
3346                 if (dentry != NULL)
3347                         path = ll_d_path(dentry, buf, PAGE_SIZE);
3348         }
3349
3350         /* The below message is checked in recovery-small.sh test_24b */
3351         CDEBUG(D_WARNING,
3352                "%s: dirty page discard: %s/fid: "DFID"/%s may get corrupted "
3353                "(rc %d)\n", ll_i2sbi(inode)->ll_fsname,
3354                s2lsi(page->mapping->host->i_sb)->lsi_lmd->lmd_dev,
3355                PFID(ll_inode2fid(inode)),
3356                (path && !IS_ERR(path)) ? path : "", ioret);
3357
3358         if (dentry != NULL)
3359                 dput(dentry);
3360
3361         if (buf != NULL)
3362                 free_page((unsigned long)buf);
3363 }
3364
3365 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
3366                         struct lov_user_md **kbuf)
3367 {
3368         struct lov_user_md      lum;
3369         ssize_t                 lum_size;
3370         ENTRY;
3371
3372         if (copy_from_user(&lum, md, sizeof(lum)))
3373                 RETURN(-EFAULT);
3374
3375         lum_size = ll_lov_user_md_size(&lum);
3376         if (lum_size < 0)
3377                 RETURN(lum_size);
3378
3379         OBD_ALLOC_LARGE(*kbuf, lum_size);
3380         if (*kbuf == NULL)
3381                 RETURN(-ENOMEM);
3382
3383         if (copy_from_user(*kbuf, md, lum_size) != 0) {
3384                 OBD_FREE_LARGE(*kbuf, lum_size);
3385                 RETURN(-EFAULT);
3386         }
3387
3388         RETURN(lum_size);
3389 }
3390
3391 /*
3392  * Compute llite root squash state after a change of root squash
3393  * configuration setting or add/remove of a lnet nid
3394  */
3395 void ll_compute_rootsquash_state(struct ll_sb_info *sbi)
3396 {
3397         struct root_squash_info *squash = &sbi->ll_squash;
3398         int i;
3399         bool matched;
3400         struct lnet_process_id id;
3401
3402         /* Update norootsquash flag */
3403         spin_lock(&squash->rsi_lock);
3404         if (list_empty(&squash->rsi_nosquash_nids))
3405                 clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
3406         else {
3407                 /* Do not apply root squash as soon as one of our NIDs is
3408                  * in the nosquash_nids list */
3409                 matched = false;
3410                 i = 0;
3411                 while (LNetGetId(i++, &id) != -ENOENT) {
3412                         if (id.nid == LNET_NID_LO_0)
3413                                 continue;
3414                         if (cfs_match_nid(id.nid, &squash->rsi_nosquash_nids)) {
3415                                 matched = true;
3416                                 break;
3417                         }
3418                 }
3419                 if (matched)
3420                         set_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
3421                 else
3422                         clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
3423         }
3424         spin_unlock(&squash->rsi_lock);
3425 }
3426
3427 /**
3428  * Parse linkea content to extract information about a given hardlink
3429  *
3430  * \param[in]   ldata      - Initialized linkea data
3431  * \param[in]   linkno     - Link identifier
3432  * \param[out]  parent_fid - The entry's parent FID
3433  * \param[out]  ln         - Entry name destination buffer
3434  *
3435  * \retval 0 on success
3436  * \retval Appropriate negative error code on failure
3437  */
3438 static int ll_linkea_decode(struct linkea_data *ldata, unsigned int linkno,
3439                             struct lu_fid *parent_fid, struct lu_name *ln)
3440 {
3441         unsigned int    idx;
3442         int             rc;
3443         ENTRY;
3444
3445         rc = linkea_init_with_rec(ldata);
3446         if (rc < 0)
3447                 RETURN(rc);
3448
3449         if (linkno >= ldata->ld_leh->leh_reccount)
3450                 /* beyond last link */
3451                 RETURN(-ENODATA);
3452
3453         linkea_first_entry(ldata);
3454         for (idx = 0; ldata->ld_lee != NULL; idx++) {
3455                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, ln,
3456                                     parent_fid);
3457                 if (idx == linkno)
3458                         break;
3459
3460                 linkea_next_entry(ldata);
3461         }
3462
3463         if (idx < linkno)
3464                 RETURN(-ENODATA);
3465
3466         RETURN(0);
3467 }
3468
3469 /**
3470  * Get parent FID and name of an identified link. Operation is performed for
3471  * a given link number, letting the caller iterate over linkno to list one or
3472  * all links of an entry.
3473  *
3474  * \param[in]     file - File descriptor against which to perform the operation
3475  * \param[in,out] arg  - User-filled structure containing the linkno to operate
3476  *                       on and the available size. It is eventually filled with
3477  *                       the requested information or left untouched on error
3478  *
3479  * \retval - 0 on success
3480  * \retval - Appropriate negative error code on failure
3481  */
3482 int ll_getparent(struct file *file, struct getparent __user *arg)
3483 {
3484         struct inode            *inode = file_inode(file);
3485         struct linkea_data      *ldata;
3486         struct lu_buf            buf = LU_BUF_NULL;
3487         struct lu_name           ln;
3488         struct lu_fid            parent_fid;
3489         __u32                    linkno;
3490         __u32                    name_size;
3491         int                      rc;
3492
3493         ENTRY;
3494
3495         if (!capable(CAP_DAC_READ_SEARCH) &&
3496             !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
3497                 RETURN(-EPERM);
3498
3499         if (get_user(name_size, &arg->gp_name_size))
3500                 RETURN(-EFAULT);
3501
3502         if (get_user(linkno, &arg->gp_linkno))
3503                 RETURN(-EFAULT);
3504
3505         if (name_size > PATH_MAX)
3506                 RETURN(-EINVAL);
3507
3508         OBD_ALLOC(ldata, sizeof(*ldata));
3509         if (ldata == NULL)
3510                 RETURN(-ENOMEM);
3511
3512         rc = linkea_data_new(ldata, &buf);
3513         if (rc < 0)
3514                 GOTO(ldata_free, rc);
3515
3516         rc = ll_xattr_list(inode, XATTR_NAME_LINK, XATTR_TRUSTED_T, buf.lb_buf,
3517                            buf.lb_len, OBD_MD_FLXATTR);
3518         if (rc < 0)
3519                 GOTO(lb_free, rc);
3520
3521         rc = ll_linkea_decode(ldata, linkno, &parent_fid, &ln);
3522         if (rc < 0)
3523                 GOTO(lb_free, rc);
3524
3525         if (ln.ln_namelen >= name_size)
3526                 GOTO(lb_free, rc = -EOVERFLOW);
3527
3528         if (copy_to_user(&arg->gp_fid, &parent_fid, sizeof(arg->gp_fid)))
3529                 GOTO(lb_free, rc = -EFAULT);
3530
3531         if (copy_to_user(&arg->gp_name, ln.ln_name, ln.ln_namelen))
3532                 GOTO(lb_free, rc = -EFAULT);
3533
3534         if (put_user('\0', arg->gp_name + ln.ln_namelen))
3535                 GOTO(lb_free, rc = -EFAULT);
3536
3537 lb_free:
3538         lu_buf_free(&buf);
3539 ldata_free:
3540         OBD_FREE(ldata, sizeof(*ldata));
3541
3542         RETURN(rc);
3543 }