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