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