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