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