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