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