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