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