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