staging/lustre/llite: remove some cl wrappers
[linux-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
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26/*
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
1dc563a6 30 * Copyright (c) 2011, 2015, Intel Corporation.
d7e09d03
PT
31 */
32/*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 *
36 * lustre/llite/llite_lib.c
37 *
38 * Lustre Light Super operations
39 */
40
41#define DEBUG_SUBSYSTEM S_LLITE
42
43#include <linux/module.h>
a9c7db39 44#include <linux/statfs.h>
d7e09d03 45#include <linux/types.h>
d7e09d03
PT
46#include <linux/mm.h>
47
67a235f5
GKH
48#include "../include/lustre_lite.h"
49#include "../include/lustre_ha.h"
50#include "../include/lustre_dlm.h"
51#include "../include/lprocfs_status.h"
52#include "../include/lustre_disk.h"
53#include "../include/lustre_param.h"
54#include "../include/lustre_log.h"
55#include "../include/cl_object.h"
56#include "../include/obd_cksum.h"
d7e09d03
PT
57#include "llite_internal.h"
58
59struct kmem_cache *ll_file_data_slab;
ae7c0f48 60struct dentry *llite_root;
fd0d04ba 61struct kset *llite_kset;
d7e09d03 62
d7e09d03
PT
63#ifndef log2
64#define log2(n) ffz(~(n))
65#endif
66
fd0d04ba 67static struct ll_sb_info *ll_init_sbi(struct super_block *sb)
d7e09d03
PT
68{
69 struct ll_sb_info *sbi = NULL;
70 unsigned long pages;
71 unsigned long lru_page_max;
72 struct sysinfo si;
73 class_uuid_t uuid;
74 int i;
d7e09d03 75
496a51bd 76 sbi = kzalloc(sizeof(*sbi), GFP_NOFS);
d7e09d03 77 if (!sbi)
0a3bdb00 78 return NULL;
d7e09d03
PT
79
80 spin_lock_init(&sbi->ll_lock);
81 mutex_init(&sbi->ll_lco.lco_lock);
82 spin_lock_init(&sbi->ll_pp_extent_lock);
83 spin_lock_init(&sbi->ll_process_lock);
84 sbi->ll_rw_stats_on = 0;
85
86 si_meminfo(&si);
87 pages = si.totalram - si.totalhigh;
5196e42c 88 lru_page_max = pages / 2;
d7e09d03 89
c52f69c5 90 /* initialize lru data */
d7e09d03
PT
91 atomic_set(&sbi->ll_cache.ccc_users, 0);
92 sbi->ll_cache.ccc_lru_max = lru_page_max;
93 atomic_set(&sbi->ll_cache.ccc_lru_left, lru_page_max);
94 spin_lock_init(&sbi->ll_cache.ccc_lru_lock);
95 INIT_LIST_HEAD(&sbi->ll_cache.ccc_lru);
96
d7e09d03
PT
97 sbi->ll_ra_info.ra_max_pages_per_file = min(pages / 32,
98 SBI_DEFAULT_READAHEAD_MAX);
99 sbi->ll_ra_info.ra_max_pages = sbi->ll_ra_info.ra_max_pages_per_file;
100 sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
101 SBI_DEFAULT_READAHEAD_WHOLE_MAX;
d7e09d03
PT
102
103 ll_generate_random_uuid(uuid);
104 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
105 CDEBUG(D_CONFIG, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
106
d7e09d03
PT
107 sbi->ll_flags |= LL_SBI_VERBOSE;
108 sbi->ll_flags |= LL_SBI_CHECKSUM;
109
110 sbi->ll_flags |= LL_SBI_LRU_RESIZE;
111
112 for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
113 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
114 pp_r_hist.oh_lock);
115 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
116 pp_w_hist.oh_lock);
117 }
118
119 /* metadata statahead is enabled by default */
120 sbi->ll_sa_max = LL_SA_RPC_DEF;
121 atomic_set(&sbi->ll_sa_total, 0);
122 atomic_set(&sbi->ll_sa_wrong, 0);
123 atomic_set(&sbi->ll_agl_total, 0);
124 sbi->ll_flags |= LL_SBI_AGL_ENABLED;
125
fd0d04ba
OD
126 sbi->ll_sb = sb;
127
0a3bdb00 128 return sbi;
d7e09d03
PT
129}
130
2d95f10e 131static void ll_free_sbi(struct super_block *sb)
d7e09d03
PT
132{
133 struct ll_sb_info *sbi = ll_s2sbi(sb);
d7e09d03 134
ad88aae0 135 kfree(sbi);
d7e09d03
PT
136}
137
d7e09d03
PT
138static int client_common_fill_super(struct super_block *sb, char *md, char *dt,
139 struct vfsmount *mnt)
140{
ea7893bb 141 struct inode *root = NULL;
d7e09d03
PT
142 struct ll_sb_info *sbi = ll_s2sbi(sb);
143 struct obd_device *obd;
d7e09d03
PT
144 struct obd_statfs *osfs = NULL;
145 struct ptlrpc_request *request = NULL;
146 struct obd_connect_data *data = NULL;
147 struct obd_uuid *uuid;
148 struct md_op_data *op_data;
149 struct lustre_md lmd;
21aef7d9 150 u64 valid;
d7e09d03 151 int size, err, checksum;
d7e09d03
PT
152
153 obd = class_name2obd(md);
154 if (!obd) {
155 CERROR("MD %s: not setup or attached\n", md);
0a3bdb00 156 return -EINVAL;
d7e09d03
PT
157 }
158
496a51bd
JL
159 data = kzalloc(sizeof(*data), GFP_NOFS);
160 if (!data)
0a3bdb00 161 return -ENOMEM;
d7e09d03 162
496a51bd
JL
163 osfs = kzalloc(sizeof(*osfs), GFP_NOFS);
164 if (!osfs) {
97903a26 165 kfree(data);
0a3bdb00 166 return -ENOMEM;
d7e09d03
PT
167 }
168
6e16818b 169 if (llite_root) {
ae7c0f48 170 err = ldebugfs_register_mountpoint(llite_root, sb, dt, md);
d7e09d03 171 if (err < 0)
ae7c0f48 172 CERROR("could not register mount in <debugfs>/lustre/llite\n");
d7e09d03
PT
173 }
174
175 /* indicate the features supported by this client */
176 data->ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_NODEVOH |
177 OBD_CONNECT_ATTRFID |
178 OBD_CONNECT_VERSION | OBD_CONNECT_BRW_SIZE |
d7e09d03
PT
179 OBD_CONNECT_CANCELSET | OBD_CONNECT_FID |
180 OBD_CONNECT_AT | OBD_CONNECT_LOV_V3 |
181 OBD_CONNECT_RMT_CLIENT | OBD_CONNECT_VBR |
182 OBD_CONNECT_FULL20 | OBD_CONNECT_64BITHASH|
183 OBD_CONNECT_EINPROGRESS |
184 OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
7fc1f831 185 OBD_CONNECT_LAYOUTLOCK |
69342b78
AS
186 OBD_CONNECT_PINGLESS |
187 OBD_CONNECT_MAX_EASIZE |
63d42578
HZ
188 OBD_CONNECT_FLOCK_DEAD |
189 OBD_CONNECT_DISP_STRIPE;
d7e09d03
PT
190
191 if (sbi->ll_flags & LL_SBI_SOM_PREVIEW)
192 data->ocd_connect_flags |= OBD_CONNECT_SOM;
193
194 if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
195 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
196#ifdef CONFIG_FS_POSIX_ACL
197 data->ocd_connect_flags |= OBD_CONNECT_ACL | OBD_CONNECT_UMASK;
198#endif
199
200 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_LIGHTWEIGHT))
201 /* flag mdc connection as lightweight, only used for test
c0894c6c
OD
202 * purpose, use with care
203 */
d7e09d03
PT
204 data->ocd_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
205
206 data->ocd_ibits_known = MDS_INODELOCK_FULL;
207 data->ocd_version = LUSTRE_VERSION_CODE;
208
209 if (sb->s_flags & MS_RDONLY)
210 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
211 if (sbi->ll_flags & LL_SBI_USER_XATTR)
212 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
213
d7e09d03
PT
214 if (sbi->ll_flags & LL_SBI_FLOCK)
215 sbi->ll_fop = &ll_file_operations_flock;
216 else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
217 sbi->ll_fop = &ll_file_operations;
218 else
219 sbi->ll_fop = &ll_file_operations_noflock;
220
221 /* real client */
222 data->ocd_connect_flags |= OBD_CONNECT_REAL;
223 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
224 data->ocd_connect_flags |= OBD_CONNECT_RMT_CLIENT_FORCE;
225
226 data->ocd_brw_size = MD_MAX_BRW_SIZE;
227
e6768831
TJ
228 err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid,
229 data, NULL);
d7e09d03 230 if (err == -EBUSY) {
2d00bd17
JP
231 LCONSOLE_ERROR_MSG(0x14f, "An MDT (md %s) is performing recovery, of which this client is not a part. Please wait for recovery to complete, abort, or time out.\n",
232 md);
34e1f2bb 233 goto out;
d7e09d03
PT
234 } else if (err) {
235 CERROR("cannot connect to %s: rc = %d\n", md, err);
34e1f2bb 236 goto out;
d7e09d03
PT
237 }
238
239 sbi->ll_md_exp->exp_connect_data = *data;
240
241 err = obd_fid_init(sbi->ll_md_exp->exp_obd, sbi->ll_md_exp,
242 LUSTRE_SEQ_METADATA);
243 if (err) {
2d00bd17
JP
244 CERROR("%s: Can't init metadata layer FID infrastructure, rc = %d\n",
245 sbi->ll_md_exp->exp_obd->obd_name, err);
34e1f2bb 246 goto out_md;
d7e09d03
PT
247 }
248
249 /* For mount, we only need fs info from MDT0, and also in DNE, it
250 * can make sure the client can be mounted as long as MDT0 is
c0894c6c
OD
251 * available
252 */
d7e09d03 253 err = obd_statfs(NULL, sbi->ll_md_exp, osfs,
e15ba45d
OD
254 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
255 OBD_STATFS_FOR_MDT0);
d7e09d03 256 if (err)
34e1f2bb 257 goto out_md_fid;
d7e09d03
PT
258
259 /* This needs to be after statfs to ensure connect has finished.
260 * Note that "data" does NOT contain the valid connect reply.
261 * If connecting to a 1.8 server there will be no LMV device, so
262 * we can access the MDC export directly and exp_connect_flags will
263 * be non-zero, but if accessing an upgraded 2.1 server it will
264 * have the correct flags filled in.
c0894c6c
OD
265 * XXX: fill in the LMV exp_connect_flags from MDC(s).
266 */
d7e09d03
PT
267 valid = exp_connect_flags(sbi->ll_md_exp) & CLIENT_CONNECT_MDT_REQD;
268 if (exp_connect_flags(sbi->ll_md_exp) != 0 &&
269 valid != CLIENT_CONNECT_MDT_REQD) {
270 char *buf;
271
496a51bd 272 buf = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
db562e81
GEHP
273 if (!buf) {
274 err = -ENOMEM;
275 goto out_md_fid;
276 }
d7e09d03
PT
277 obd_connect_flags2str(buf, PAGE_CACHE_SIZE,
278 valid ^ CLIENT_CONNECT_MDT_REQD, ",");
2d00bd17 279 LCONSOLE_ERROR_MSG(0x170, "Server %s does not support feature(s) needed for correct operation of this client (%s). Please upgrade server or downgrade client.\n",
d7e09d03 280 sbi->ll_md_exp->exp_obd->obd_name, buf);
97903a26 281 kfree(buf);
34e1f2bb
JL
282 err = -EPROTO;
283 goto out_md_fid;
d7e09d03
PT
284 }
285
286 size = sizeof(*data);
287 err = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_CONN_DATA),
288 KEY_CONN_DATA, &size, data, NULL);
289 if (err) {
290 CERROR("%s: Get connect data failed: rc = %d\n",
291 sbi->ll_md_exp->exp_obd->obd_name, err);
34e1f2bb 292 goto out_md_fid;
d7e09d03
PT
293 }
294
295 LASSERT(osfs->os_bsize);
296 sb->s_blocksize = osfs->os_bsize;
297 sb->s_blocksize_bits = log2(osfs->os_bsize);
298 sb->s_magic = LL_SUPER_MAGIC;
299 sb->s_maxbytes = MAX_LFS_FILESIZE;
300 sbi->ll_namelen = osfs->os_namelen;
d7e09d03
PT
301
302 if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
303 !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
2d00bd17 304 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
d7e09d03
PT
305 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
306 }
307
308 if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
d7e09d03 309 sb->s_flags |= MS_POSIXACL;
d7e09d03
PT
310 sbi->ll_flags |= LL_SBI_ACL;
311 } else {
312 LCONSOLE_INFO("client wants to enable acl, but mdt not!\n");
d7e09d03 313 sb->s_flags &= ~MS_POSIXACL;
d7e09d03
PT
314 sbi->ll_flags &= ~LL_SBI_ACL;
315 }
316
317 if (data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) {
318 if (!(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
319 sbi->ll_flags |= LL_SBI_RMT_CLIENT;
320 LCONSOLE_INFO("client is set as remote by default.\n");
321 }
322 } else {
323 if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
324 sbi->ll_flags &= ~LL_SBI_RMT_CLIENT;
2d00bd17 325 LCONSOLE_INFO("client claims to be remote, but server rejected, forced to be local.\n");
d7e09d03
PT
326 }
327 }
328
d7e09d03
PT
329 if (data->ocd_connect_flags & OBD_CONNECT_64BITHASH)
330 sbi->ll_flags |= LL_SBI_64BIT_HASH;
331
332 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
333 sbi->ll_md_brw_size = data->ocd_brw_size;
334 else
335 sbi->ll_md_brw_size = PAGE_CACHE_SIZE;
336
337 if (data->ocd_connect_flags & OBD_CONNECT_LAYOUTLOCK) {
338 LCONSOLE_INFO("Layout lock feature supported.\n");
339 sbi->ll_flags |= LL_SBI_LAYOUT_LOCK;
340 }
341
7fc1f831
AP
342 if (data->ocd_ibits_known & MDS_INODELOCK_XATTR) {
343 if (!(data->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)) {
344 LCONSOLE_INFO(
345 "%s: disabling xattr cache due to unknown maximum xattr size.\n",
346 dt);
347 } else {
348 sbi->ll_flags |= LL_SBI_XATTR_CACHE;
349 sbi->ll_xattr_cache_enabled = 1;
350 }
351 }
352
d7e09d03
PT
353 obd = class_name2obd(dt);
354 if (!obd) {
355 CERROR("DT %s: not setup or attached\n", dt);
34e1f2bb
JL
356 err = -ENODEV;
357 goto out_md_fid;
d7e09d03
PT
358 }
359
360 data->ocd_connect_flags = OBD_CONNECT_GRANT | OBD_CONNECT_VERSION |
361 OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
362 OBD_CONNECT_CANCELSET | OBD_CONNECT_FID |
363 OBD_CONNECT_SRVLOCK | OBD_CONNECT_TRUNCLOCK|
364 OBD_CONNECT_AT | OBD_CONNECT_RMT_CLIENT |
365 OBD_CONNECT_OSS_CAPA | OBD_CONNECT_VBR|
366 OBD_CONNECT_FULL20 | OBD_CONNECT_64BITHASH |
367 OBD_CONNECT_MAXBYTES |
368 OBD_CONNECT_EINPROGRESS |
369 OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
370 OBD_CONNECT_LAYOUTLOCK | OBD_CONNECT_PINGLESS;
371
372 if (sbi->ll_flags & LL_SBI_SOM_PREVIEW)
373 data->ocd_connect_flags |= OBD_CONNECT_SOM;
374
375 if (!OBD_FAIL_CHECK(OBD_FAIL_OSC_CONNECT_CKSUM)) {
376 /* OBD_CONNECT_CKSUM should always be set, even if checksums are
377 * disabled by default, because it can still be enabled on the
40cc864a 378 * fly via /sys. As a consequence, we still need to come to an
c0894c6c
OD
379 * agreement on the supported algorithms at connect time
380 */
d7e09d03
PT
381 data->ocd_connect_flags |= OBD_CONNECT_CKSUM;
382
383 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
384 data->ocd_cksum_types = OBD_CKSUM_ADLER;
385 else
386 data->ocd_cksum_types = cksum_types_supported_client();
387 }
388
389 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
390 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
391 data->ocd_connect_flags |= OBD_CONNECT_RMT_CLIENT_FORCE;
392
2d00bd17
JP
393 CDEBUG(D_RPCTRACE, "ocd_connect_flags: %#llx ocd_version: %d ocd_grant: %d\n",
394 data->ocd_connect_flags,
d7e09d03
PT
395 data->ocd_version, data->ocd_grant);
396
397 obd->obd_upcall.onu_owner = &sbi->ll_lco;
398 obd->obd_upcall.onu_upcall = cl_ocd_update;
399
400 data->ocd_brw_size = DT_MAX_BRW_SIZE;
401
402 err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, data,
403 NULL);
404 if (err == -EBUSY) {
2d00bd17
JP
405 LCONSOLE_ERROR_MSG(0x150, "An OST (dt %s) is performing recovery, of which this client is not a part. Please wait for recovery to complete, abort, or time out.\n",
406 dt);
34e1f2bb 407 goto out_md;
d7e09d03
PT
408 } else if (err) {
409 CERROR("%s: Cannot connect to %s: rc = %d\n",
410 sbi->ll_dt_exp->exp_obd->obd_name, dt, err);
34e1f2bb 411 goto out_md;
d7e09d03
PT
412 }
413
414 sbi->ll_dt_exp->exp_connect_data = *data;
415
416 err = obd_fid_init(sbi->ll_dt_exp->exp_obd, sbi->ll_dt_exp,
417 LUSTRE_SEQ_METADATA);
418 if (err) {
2d00bd17
JP
419 CERROR("%s: Can't init data layer FID infrastructure, rc = %d\n",
420 sbi->ll_dt_exp->exp_obd->obd_name, err);
34e1f2bb 421 goto out_dt;
d7e09d03
PT
422 }
423
424 mutex_lock(&sbi->ll_lco.lco_lock);
425 sbi->ll_lco.lco_flags = data->ocd_connect_flags;
426 sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
427 sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
428 mutex_unlock(&sbi->ll_lco.lco_lock);
429
430 fid_zero(&sbi->ll_root_fid);
ef2e0f55 431 err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid);
d7e09d03
PT
432 if (err) {
433 CERROR("cannot mds_connect: rc = %d\n", err);
34e1f2bb 434 goto out_lock_cn_cb;
d7e09d03
PT
435 }
436 if (!fid_is_sane(&sbi->ll_root_fid)) {
437 CERROR("%s: Invalid root fid "DFID" during mount\n",
438 sbi->ll_md_exp->exp_obd->obd_name,
439 PFID(&sbi->ll_root_fid));
34e1f2bb
JL
440 err = -EINVAL;
441 goto out_lock_cn_cb;
d7e09d03
PT
442 }
443 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
444
445 sb->s_op = &lustre_super_operations;
446#if THREAD_SIZE >= 8192 /*b=17630*/
447 sb->s_export_op = &lustre_export_operations;
448#endif
449
450 /* make root inode
c0894c6c
OD
451 * XXX: move this to after cbd setup?
452 */
ef2e0f55 453 valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS;
d7e09d03
PT
454 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
455 valid |= OBD_MD_FLRMTPERM;
456 else if (sbi->ll_flags & LL_SBI_ACL)
457 valid |= OBD_MD_FLACL;
458
496a51bd
JL
459 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
460 if (!op_data) {
34e1f2bb
JL
461 err = -ENOMEM;
462 goto out_lock_cn_cb;
463 }
d7e09d03
PT
464
465 op_data->op_fid1 = sbi->ll_root_fid;
466 op_data->op_mode = 0;
d7e09d03
PT
467 op_data->op_valid = valid;
468
469 err = md_getattr(sbi->ll_md_exp, op_data, &request);
97903a26 470 kfree(op_data);
d7e09d03
PT
471 if (err) {
472 CERROR("%s: md_getattr failed for root: rc = %d\n",
473 sbi->ll_md_exp->exp_obd->obd_name, err);
34e1f2bb 474 goto out_lock_cn_cb;
d7e09d03
PT
475 }
476
477 err = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
478 sbi->ll_md_exp, &lmd);
479 if (err) {
480 CERROR("failed to understand root inode md: rc = %d\n", err);
481 ptlrpc_req_finished(request);
34e1f2bb 482 goto out_lock_cn_cb;
d7e09d03
PT
483 }
484
485 LASSERT(fid_is_sane(&sbi->ll_root_fid));
486 root = ll_iget(sb, cl_fid_build_ino(&sbi->ll_root_fid,
c1e2699d 487 sbi->ll_flags & LL_SBI_32BIT_API),
d7e09d03
PT
488 &lmd);
489 md_free_lustre_md(sbi->ll_md_exp, &lmd);
490 ptlrpc_req_finished(request);
491
020ecc6f 492 if (!(root)) {
d7e09d03
PT
493 if (lmd.lsm)
494 obd_free_memmd(sbi->ll_dt_exp, &lmd.lsm);
495#ifdef CONFIG_FS_POSIX_ACL
496 if (lmd.posix_acl) {
497 posix_acl_release(lmd.posix_acl);
498 lmd.posix_acl = NULL;
499 }
500#endif
020ecc6f 501 err = -EBADF;
d7e09d03 502 CERROR("lustre_lite: bad iget4 for root\n");
34e1f2bb 503 goto out_root;
d7e09d03
PT
504 }
505
506 err = ll_close_thread_start(&sbi->ll_lcq);
507 if (err) {
508 CERROR("cannot start close thread: rc %d\n", err);
34e1f2bb 509 goto out_root;
d7e09d03
PT
510 }
511
512#ifdef CONFIG_FS_POSIX_ACL
513 if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
514 rct_init(&sbi->ll_rct);
515 et_init(&sbi->ll_et);
516 }
517#endif
518
519 checksum = sbi->ll_flags & LL_SBI_CHECKSUM;
520 err = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
521 KEY_CHECKSUM, sizeof(checksum), &checksum,
522 NULL);
523 cl_sb_init(sb);
524
525 err = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CACHE_SET),
526 KEY_CACHE_SET, sizeof(sbi->ll_cache),
527 &sbi->ll_cache, NULL);
528
529 sb->s_root = d_make_root(root);
6e16818b 530 if (!sb->s_root) {
d7e09d03 531 CERROR("%s: can't make root dentry\n",
e15ba45d 532 ll_get_fsname(sb, NULL, 0));
34e1f2bb 533 err = -ENOMEM;
caf382fe 534 goto out_lock_cn_cb;
d7e09d03
PT
535 }
536
d7e09d03
PT
537 sbi->ll_sdev_orig = sb->s_dev;
538
539 /* We set sb->s_dev equal on all lustre clients in order to support
540 * NFS export clustering. NFSD requires that the FSID be the same
c0894c6c
OD
541 * on all clients.
542 */
d7e09d03 543 /* s_dev is also used in lt_compare() to compare two fs, but that is
c0894c6c
OD
544 * only a node-local comparison.
545 */
d7e09d03 546 uuid = obd_get_uuid(sbi->ll_md_exp);
6e16818b 547 if (uuid) {
d7e09d03 548 sb->s_dev = get_uuid2int(uuid->uuid, strlen(uuid->uuid));
bd994071
FY
549 get_uuid2fsid(uuid->uuid, strlen(uuid->uuid), &sbi->ll_fsid);
550 }
d7e09d03 551
081825f5
JL
552 kfree(data);
553 kfree(osfs);
d7e09d03 554
0a3bdb00 555 return err;
d7e09d03 556out_root:
ddafd514 557 iput(root);
d7e09d03
PT
558out_lock_cn_cb:
559 obd_fid_fini(sbi->ll_dt_exp->exp_obd);
560out_dt:
561 obd_disconnect(sbi->ll_dt_exp);
562 sbi->ll_dt_exp = NULL;
563 /* Make sure all OScs are gone, since cl_cache is accessing sbi. */
564 obd_zombie_barrier();
565out_md_fid:
566 obd_fid_fini(sbi->ll_md_exp->exp_obd);
567out_md:
568 obd_disconnect(sbi->ll_md_exp);
569 sbi->ll_md_exp = NULL;
570out:
081825f5
JL
571 kfree(data);
572 kfree(osfs);
ae7c0f48 573 ldebugfs_unregister_mountpoint(sbi);
d7e09d03
PT
574 return err;
575}
576
577int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
578{
579 int size, rc;
580
581 *lmmsize = obd_size_diskmd(sbi->ll_dt_exp, NULL);
582 size = sizeof(int);
583 rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_MAX_EASIZE),
584 KEY_MAX_EASIZE, &size, lmmsize, NULL);
585 if (rc)
4f211c20 586 CERROR("Get max mdsize error rc %d\n", rc);
d7e09d03 587
0a3bdb00 588 return rc;
44779340
BB
589}
590
591int ll_get_default_mdsize(struct ll_sb_info *sbi, int *lmmsize)
592{
593 int size, rc;
594
595 size = sizeof(int);
596 rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_DEFAULT_EASIZE),
e15ba45d 597 KEY_DEFAULT_EASIZE, &size, lmmsize, NULL);
44779340
BB
598 if (rc)
599 CERROR("Get default mdsize error rc %d\n", rc);
600
601 return rc;
602}
603
2d95f10e 604static void client_common_put_super(struct super_block *sb)
d7e09d03
PT
605{
606 struct ll_sb_info *sbi = ll_s2sbi(sb);
d7e09d03
PT
607
608#ifdef CONFIG_FS_POSIX_ACL
609 if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
610 et_fini(&sbi->ll_et);
611 rct_fini(&sbi->ll_rct);
612 }
613#endif
614
615 ll_close_thread_shutdown(sbi->ll_lcq);
616
617 cl_sb_fini(sb);
618
d7e09d03
PT
619 obd_fid_fini(sbi->ll_dt_exp->exp_obd);
620 obd_disconnect(sbi->ll_dt_exp);
621 sbi->ll_dt_exp = NULL;
622 /* wait till all OSCs are gone, since cl_cache is accessing sbi.
c0894c6c
OD
623 * see LU-2543.
624 */
d7e09d03
PT
625 obd_zombie_barrier();
626
ae7c0f48 627 ldebugfs_unregister_mountpoint(sbi);
d7e09d03
PT
628
629 obd_fid_fini(sbi->ll_md_exp->exp_obd);
630 obd_disconnect(sbi->ll_md_exp);
631 sbi->ll_md_exp = NULL;
d7e09d03
PT
632}
633
634void ll_kill_super(struct super_block *sb)
635{
636 struct ll_sb_info *sbi;
637
d7e09d03
PT
638 /* not init sb ?*/
639 if (!(sb->s_flags & MS_ACTIVE))
640 return;
641
642 sbi = ll_s2sbi(sb);
e6768831
TJ
643 /* we need to restore s_dev from changed for clustered NFS before
644 * put_super because new kernels have cached s_dev and change sb->s_dev
c0894c6c
OD
645 * in put_super not affected real removing devices
646 */
65fb55d1 647 if (sbi) {
d7e09d03 648 sb->s_dev = sbi->ll_sdev_orig;
65fb55d1
NY
649 sbi->ll_umounting = 1;
650 }
d7e09d03
PT
651}
652
d7e09d03
PT
653static inline int ll_set_opt(const char *opt, char *data, int fl)
654{
655 if (strncmp(opt, data, strlen(opt)) != 0)
fbe7c6c7 656 return 0;
d7e09d03 657 else
fbe7c6c7 658 return fl;
d7e09d03
PT
659}
660
661/* non-client-specific mount options are parsed in lmd_parse */
662static int ll_options(char *options, int *flags)
663{
664 int tmp;
665 char *s1 = options, *s2;
d7e09d03
PT
666
667 if (!options)
0a3bdb00 668 return 0;
d7e09d03
PT
669
670 CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
671
672 while (*s1) {
673 CDEBUG(D_SUPER, "next opt=%s\n", s1);
674 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
675 if (tmp) {
676 *flags |= tmp;
677 goto next;
678 }
679 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
680 if (tmp) {
681 *flags |= tmp;
682 goto next;
683 }
684 tmp = ll_set_opt("localflock", s1, LL_SBI_LOCALFLOCK);
685 if (tmp) {
686 *flags |= tmp;
687 goto next;
688 }
689 tmp = ll_set_opt("noflock", s1, LL_SBI_FLOCK|LL_SBI_LOCALFLOCK);
690 if (tmp) {
691 *flags &= ~tmp;
692 goto next;
693 }
694 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
695 if (tmp) {
696 *flags |= tmp;
697 goto next;
698 }
699 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
700 if (tmp) {
701 *flags &= ~tmp;
702 goto next;
703 }
d7e09d03
PT
704 tmp = ll_set_opt("remote_client", s1, LL_SBI_RMT_CLIENT);
705 if (tmp) {
706 *flags |= tmp;
707 goto next;
708 }
709 tmp = ll_set_opt("user_fid2path", s1, LL_SBI_USER_FID2PATH);
710 if (tmp) {
711 *flags |= tmp;
712 goto next;
713 }
714 tmp = ll_set_opt("nouser_fid2path", s1, LL_SBI_USER_FID2PATH);
715 if (tmp) {
716 *flags &= ~tmp;
717 goto next;
718 }
719
720 tmp = ll_set_opt("checksum", s1, LL_SBI_CHECKSUM);
721 if (tmp) {
722 *flags |= tmp;
723 goto next;
724 }
725 tmp = ll_set_opt("nochecksum", s1, LL_SBI_CHECKSUM);
726 if (tmp) {
727 *flags &= ~tmp;
728 goto next;
729 }
730 tmp = ll_set_opt("lruresize", s1, LL_SBI_LRU_RESIZE);
731 if (tmp) {
732 *flags |= tmp;
733 goto next;
734 }
735 tmp = ll_set_opt("nolruresize", s1, LL_SBI_LRU_RESIZE);
736 if (tmp) {
737 *flags &= ~tmp;
738 goto next;
739 }
740 tmp = ll_set_opt("lazystatfs", s1, LL_SBI_LAZYSTATFS);
741 if (tmp) {
742 *flags |= tmp;
743 goto next;
744 }
745 tmp = ll_set_opt("nolazystatfs", s1, LL_SBI_LAZYSTATFS);
746 if (tmp) {
747 *flags &= ~tmp;
748 goto next;
749 }
750 tmp = ll_set_opt("som_preview", s1, LL_SBI_SOM_PREVIEW);
751 if (tmp) {
752 *flags |= tmp;
753 goto next;
754 }
755 tmp = ll_set_opt("32bitapi", s1, LL_SBI_32BIT_API);
756 if (tmp) {
757 *flags |= tmp;
758 goto next;
759 }
760 tmp = ll_set_opt("verbose", s1, LL_SBI_VERBOSE);
761 if (tmp) {
762 *flags |= tmp;
763 goto next;
764 }
765 tmp = ll_set_opt("noverbose", s1, LL_SBI_VERBOSE);
766 if (tmp) {
767 *flags &= ~tmp;
768 goto next;
769 }
770 LCONSOLE_ERROR_MSG(0x152, "Unknown option '%s', won't mount.\n",
771 s1);
0a3bdb00 772 return -EINVAL;
d7e09d03
PT
773
774next:
775 /* Find next opt */
776 s2 = strchr(s1, ',');
6e16818b 777 if (!s2)
d7e09d03
PT
778 break;
779 s1 = s2 + 1;
780 }
0a3bdb00 781 return 0;
d7e09d03
PT
782}
783
784void ll_lli_init(struct ll_inode_info *lli)
785{
786 lli->lli_inode_magic = LLI_INODE_MAGIC;
787 lli->lli_flags = 0;
788 lli->lli_ioepoch = 0;
789 lli->lli_maxbytes = MAX_LFS_FILESIZE;
790 spin_lock_init(&lli->lli_lock);
791 lli->lli_posix_acl = NULL;
792 lli->lli_remote_perms = NULL;
793 mutex_init(&lli->lli_rmtperm_mutex);
794 /* Do not set lli_fid, it has been initialized already. */
795 fid_zero(&lli->lli_pfid);
796 INIT_LIST_HEAD(&lli->lli_close_list);
d7e09d03
PT
797 lli->lli_rmtperm_time = 0;
798 lli->lli_pending_och = NULL;
799 lli->lli_mds_read_och = NULL;
800 lli->lli_mds_write_och = NULL;
801 lli->lli_mds_exec_och = NULL;
802 lli->lli_open_fd_read_count = 0;
803 lli->lli_open_fd_write_count = 0;
804 lli->lli_open_fd_exec_count = 0;
805 mutex_init(&lli->lli_och_mutex);
806 spin_lock_init(&lli->lli_agl_lock);
807 lli->lli_has_smd = false;
09aed8a5
JX
808 spin_lock_init(&lli->lli_layout_lock);
809 ll_layout_version_set(lli, LL_LAYOUT_GEN_NONE);
d7e09d03
PT
810 lli->lli_clob = NULL;
811
7fc1f831
AP
812 init_rwsem(&lli->lli_xattrs_list_rwsem);
813 mutex_init(&lli->lli_xattrs_enq_lock);
814
d7e09d03
PT
815 LASSERT(lli->lli_vfs_inode.i_mode != 0);
816 if (S_ISDIR(lli->lli_vfs_inode.i_mode)) {
817 mutex_init(&lli->lli_readdir_mutex);
818 lli->lli_opendir_key = NULL;
819 lli->lli_sai = NULL;
d7e09d03
PT
820 spin_lock_init(&lli->lli_sa_lock);
821 lli->lli_opendir_pid = 0;
822 } else {
47a57bde 823 mutex_init(&lli->lli_size_mutex);
d7e09d03
PT
824 lli->lli_symlink_name = NULL;
825 init_rwsem(&lli->lli_trunc_sem);
826 mutex_init(&lli->lli_write_mutex);
827 init_rwsem(&lli->lli_glimpse_sem);
828 lli->lli_glimpse_time = 0;
829 INIT_LIST_HEAD(&lli->lli_agl_list);
830 lli->lli_agl_index = 0;
831 lli->lli_async_rc = 0;
d7e09d03
PT
832 }
833 mutex_init(&lli->lli_layout_mutex);
834}
835
836static inline int ll_bdi_register(struct backing_dev_info *bdi)
837{
838 static atomic_t ll_bdi_num = ATOMIC_INIT(0);
839
840 bdi->name = "lustre";
841 return bdi_register(bdi, NULL, "lustre-%d",
842 atomic_inc_return(&ll_bdi_num));
843}
844
845int ll_fill_super(struct super_block *sb, struct vfsmount *mnt)
846{
847 struct lustre_profile *lprof = NULL;
848 struct lustre_sb_info *lsi = s2lsi(sb);
849 struct ll_sb_info *sbi;
850 char *dt = NULL, *md = NULL;
851 char *profilenm = get_profile_name(sb);
852 struct config_llog_instance *cfg;
d7e09d03 853 int err;
d7e09d03
PT
854
855 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
856
496a51bd
JL
857 cfg = kzalloc(sizeof(*cfg), GFP_NOFS);
858 if (!cfg)
0a3bdb00 859 return -ENOMEM;
d7e09d03
PT
860
861 try_module_get(THIS_MODULE);
862
863 /* client additional sb info */
fd0d04ba 864 lsi->lsi_llsbi = sbi = ll_init_sbi(sb);
d7e09d03
PT
865 if (!sbi) {
866 module_put(THIS_MODULE);
97903a26 867 kfree(cfg);
0a3bdb00 868 return -ENOMEM;
d7e09d03
PT
869 }
870
871 err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
872 if (err)
34e1f2bb 873 goto out_free;
d7e09d03
PT
874
875 err = bdi_init(&lsi->lsi_bdi);
876 if (err)
34e1f2bb 877 goto out_free;
d7e09d03 878 lsi->lsi_flags |= LSI_BDI_INITIALIZED;
b4caecd4 879 lsi->lsi_bdi.capabilities = 0;
d7e09d03
PT
880 err = ll_bdi_register(&lsi->lsi_bdi);
881 if (err)
34e1f2bb 882 goto out_free;
d7e09d03
PT
883
884 sb->s_bdi = &lsi->lsi_bdi;
3ea8f3bc
LS
885 /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
886 sb->s_d_op = &ll_d_ops;
d7e09d03
PT
887
888 /* Generate a string unique to this super, in case some joker tries
c0894c6c
OD
889 * to mount the same fs at two mount points.
890 * Use the address of the super itself.
891 */
d7e09d03
PT
892 cfg->cfg_instance = sb;
893 cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
894 cfg->cfg_callback = class_config_llog_handler;
895 /* set up client obds */
896 err = lustre_process_log(sb, profilenm, cfg);
897 if (err < 0) {
898 CERROR("Unable to process log: %d\n", err);
34e1f2bb 899 goto out_free;
d7e09d03
PT
900 }
901
902 /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
903 lprof = class_get_profile(profilenm);
6e16818b 904 if (!lprof) {
2d00bd17
JP
905 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be read from the MGS. Does that filesystem exist?\n",
906 profilenm);
34e1f2bb
JL
907 err = -EINVAL;
908 goto out_free;
d7e09d03
PT
909 }
910 CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
911 lprof->lp_md, lprof->lp_dt);
912
95745e9b 913 dt = kasprintf(GFP_NOFS, "%s-%p", lprof->lp_dt, cfg->cfg_instance);
34e1f2bb
JL
914 if (!dt) {
915 err = -ENOMEM;
916 goto out_free;
917 }
d7e09d03 918
ef2e1a44 919 md = kasprintf(GFP_NOFS, "%s-%p", lprof->lp_md, cfg->cfg_instance);
34e1f2bb
JL
920 if (!md) {
921 err = -ENOMEM;
922 goto out_free;
923 }
d7e09d03
PT
924
925 /* connections, registrations, sb setup */
926 err = client_common_fill_super(sb, md, dt, mnt);
927
928out_free:
0550db92 929 kfree(md);
930 kfree(dt);
d7e09d03
PT
931 if (err)
932 ll_put_super(sb);
933 else if (sbi->ll_flags & LL_SBI_VERBOSE)
934 LCONSOLE_WARN("Mounted %s\n", profilenm);
935
97903a26 936 kfree(cfg);
0a3bdb00 937 return err;
d7e09d03
PT
938} /* ll_fill_super */
939
d7e09d03
PT
940void ll_put_super(struct super_block *sb)
941{
7d4bae45 942 struct config_llog_instance cfg, params_cfg;
d7e09d03
PT
943 struct obd_device *obd;
944 struct lustre_sb_info *lsi = s2lsi(sb);
945 struct ll_sb_info *sbi = ll_s2sbi(sb);
946 char *profilenm = get_profile_name(sb);
c52f69c5 947 int next, force = 1;
d7e09d03
PT
948
949 CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
950
d7e09d03
PT
951 cfg.cfg_instance = sb;
952 lustre_end_log(sb, profilenm, &cfg);
953
7d4bae45
AB
954 params_cfg.cfg_instance = sb;
955 lustre_end_log(sb, PARAMS_FILENAME, &params_cfg);
956
d7e09d03
PT
957 if (sbi->ll_md_exp) {
958 obd = class_exp2obd(sbi->ll_md_exp);
959 if (obd)
960 force = obd->obd_force;
961 }
962
d7e09d03 963 /* We need to set force before the lov_disconnect in
c0894c6c
OD
964 * lustre_common_put_super, since l_d cleans up osc's as well.
965 */
d7e09d03
PT
966 if (force) {
967 next = 0;
968 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
969 &next)) != NULL) {
970 obd->obd_force = force;
971 }
972 }
973
974 if (sbi->ll_lcq) {
975 /* Only if client_common_fill_super succeeded */
976 client_common_put_super(sb);
977 }
978
979 next = 0;
a15dbf99 980 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)))
d7e09d03 981 class_manual_cleanup(obd);
d7e09d03
PT
982
983 if (sbi->ll_flags & LL_SBI_VERBOSE)
984 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
985
986 if (profilenm)
987 class_del_profile(profilenm);
988
989 if (lsi->lsi_flags & LSI_BDI_INITIALIZED) {
990 bdi_destroy(&lsi->lsi_bdi);
991 lsi->lsi_flags &= ~LSI_BDI_INITIALIZED;
992 }
993
994 ll_free_sbi(sb);
995 lsi->lsi_llsbi = NULL;
996
997 lustre_common_put_super(sb);
998
26f98e82
JX
999 cl_env_cache_purge(~0);
1000
d7e09d03 1001 module_put(THIS_MODULE);
d7e09d03
PT
1002} /* client_put_super */
1003
1004struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock)
1005{
1006 struct inode *inode = NULL;
1007
1008 /* NOTE: we depend on atomic igrab() -bzzz */
1009 lock_res_and_lock(lock);
1010 if (lock->l_resource->lr_lvb_inode) {
aff9d8e8 1011 struct ll_inode_info *lli;
cf29a7b6 1012
d7e09d03
PT
1013 lli = ll_i2info(lock->l_resource->lr_lvb_inode);
1014 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1015 inode = igrab(lock->l_resource->lr_lvb_inode);
1016 } else {
1017 inode = lock->l_resource->lr_lvb_inode;
1018 LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ? D_INFO :
2d00bd17 1019 D_WARNING, lock, "lr_lvb_inode %p is bogus: magic %08x",
d7e09d03
PT
1020 lock->l_resource->lr_lvb_inode,
1021 lli->lli_inode_magic);
1022 inode = NULL;
1023 }
1024 }
1025 unlock_res_and_lock(lock);
1026 return inode;
1027}
1028
d7e09d03
PT
1029void ll_clear_inode(struct inode *inode)
1030{
1031 struct ll_inode_info *lli = ll_i2info(inode);
1032 struct ll_sb_info *sbi = ll_i2sbi(inode);
d7e09d03
PT
1033
1034 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1035 inode->i_generation, inode);
1036
1037 if (S_ISDIR(inode->i_mode)) {
1038 /* these should have been cleared in ll_file_release */
6e16818b
OD
1039 LASSERT(!lli->lli_opendir_key);
1040 LASSERT(!lli->lli_sai);
d7e09d03
PT
1041 LASSERT(lli->lli_opendir_pid == 0);
1042 }
1043
ae5ef67b 1044 spin_lock(&lli->lli_lock);
d7e09d03 1045 ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
ae5ef67b 1046 spin_unlock(&lli->lli_lock);
d7e09d03
PT
1047 md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1048
1049 LASSERT(!lli->lli_open_fd_write_count);
1050 LASSERT(!lli->lli_open_fd_read_count);
1051 LASSERT(!lli->lli_open_fd_exec_count);
1052
1053 if (lli->lli_mds_write_och)
1054 ll_md_real_close(inode, FMODE_WRITE);
1055 if (lli->lli_mds_exec_och)
1056 ll_md_real_close(inode, FMODE_EXEC);
1057 if (lli->lli_mds_read_och)
1058 ll_md_real_close(inode, FMODE_READ);
1059
a5cb8880 1060 if (S_ISLNK(inode->i_mode)) {
97903a26 1061 kfree(lli->lli_symlink_name);
d7e09d03
PT
1062 lli->lli_symlink_name = NULL;
1063 }
1064
7fc1f831
AP
1065 ll_xattr_cache_destroy(inode);
1066
d7e09d03 1067 if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
6e16818b 1068 LASSERT(!lli->lli_posix_acl);
d7e09d03
PT
1069 if (lli->lli_remote_perms) {
1070 free_rmtperm_hash(lli->lli_remote_perms);
1071 lli->lli_remote_perms = NULL;
1072 }
1073 }
1074#ifdef CONFIG_FS_POSIX_ACL
1075 else if (lli->lli_posix_acl) {
1076 LASSERT(atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
6e16818b 1077 LASSERT(!lli->lli_remote_perms);
d7e09d03
PT
1078 posix_acl_release(lli->lli_posix_acl);
1079 lli->lli_posix_acl = NULL;
1080 }
1081#endif
1082 lli->lli_inode_magic = LLI_INODE_DEAD;
1083
d7e09d03
PT
1084 if (!S_ISDIR(inode->i_mode))
1085 LASSERT(list_empty(&lli->lli_agl_list));
1086
1087 /*
1088 * XXX This has to be done before lsm is freed below, because
1089 * cl_object still uses inode lsm.
1090 */
1091 cl_inode_fini(inode);
1092 lli->lli_has_smd = false;
d7e09d03
PT
1093}
1094
b81f9b6d
OD
1095#define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
1096
3d3ab8cc 1097static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data,
e15ba45d 1098 struct md_open_data **mod)
d7e09d03
PT
1099{
1100 struct lustre_md md;
2b0143b5 1101 struct inode *inode = d_inode(dentry);
d7e09d03
PT
1102 struct ll_sb_info *sbi = ll_i2sbi(inode);
1103 struct ptlrpc_request *request = NULL;
1104 int rc, ia_valid;
d7e09d03
PT
1105
1106 op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1107 LUSTRE_OPC_ANY, NULL);
1108 if (IS_ERR(op_data))
0a3bdb00 1109 return PTR_ERR(op_data);
d7e09d03
PT
1110
1111 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL, 0,
1112 &request, mod);
1113 if (rc) {
1114 ptlrpc_req_finished(request);
1115 if (rc == -ENOENT) {
1116 clear_nlink(inode);
1117 /* Unlinked special device node? Or just a race?
c0894c6c
OD
1118 * Pretend we did everything.
1119 */
d7e09d03
PT
1120 if (!S_ISREG(inode->i_mode) &&
1121 !S_ISDIR(inode->i_mode)) {
1122 ia_valid = op_data->op_attr.ia_valid;
1123 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1124 rc = simple_setattr(dentry, &op_data->op_attr);
1125 op_data->op_attr.ia_valid = ia_valid;
1126 }
1127 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1128 CERROR("md_setattr fails: rc = %d\n", rc);
1129 }
0a3bdb00 1130 return rc;
d7e09d03
PT
1131 }
1132
1133 rc = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
1134 sbi->ll_md_exp, &md);
1135 if (rc) {
1136 ptlrpc_req_finished(request);
0a3bdb00 1137 return rc;
d7e09d03
PT
1138 }
1139
251c4317 1140 ia_valid = op_data->op_attr.ia_valid;
ef2e0f55 1141 /* inode size will be in cl_setattr_ost, can't do it now since dirty
c0894c6c
OD
1142 * cache is not cleared yet.
1143 */
251c4317
JH
1144 op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
1145 rc = simple_setattr(dentry, &op_data->op_attr);
1146 op_data->op_attr.ia_valid = ia_valid;
1147
d7e09d03
PT
1148 /* Extract epoch data if obtained. */
1149 op_data->op_handle = md.body->handle;
1150 op_data->op_ioepoch = md.body->ioepoch;
1151
1152 ll_update_inode(inode, &md);
1153 ptlrpc_req_finished(request);
1154
0a3bdb00 1155 return rc;
d7e09d03
PT
1156}
1157
1158/* Close IO epoch and send Size-on-MDS attribute update. */
1159static int ll_setattr_done_writing(struct inode *inode,
1160 struct md_op_data *op_data,
1161 struct md_open_data *mod)
1162{
1163 struct ll_inode_info *lli = ll_i2info(inode);
1164 int rc = 0;
d7e09d03 1165
d7e09d03 1166 if (!S_ISREG(inode->i_mode))
0a3bdb00 1167 return 0;
d7e09d03 1168
b0f5aad5 1169 CDEBUG(D_INODE, "Epoch %llu closed on "DFID" for truncate\n",
d7e09d03
PT
1170 op_data->op_ioepoch, PFID(&lli->lli_fid));
1171
1172 op_data->op_flags = MF_EPOCH_CLOSE;
1173 ll_done_writing_attr(inode, op_data);
1174 ll_pack_inode2opdata(inode, op_data, NULL);
1175
1176 rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, mod);
9d927bc5 1177 if (rc == -EAGAIN)
d7e09d03 1178 /* MDS has instructed us to obtain Size-on-MDS attribute
c0894c6c
OD
1179 * from OSTs and send setattr to back to MDS.
1180 */
d7e09d03 1181 rc = ll_som_update(inode, op_data);
9d927bc5 1182 else if (rc)
d7e09d03
PT
1183 CERROR("inode %lu mdc truncate failed: rc = %d\n",
1184 inode->i_ino, rc);
0a3bdb00 1185 return rc;
d7e09d03
PT
1186}
1187
d7e09d03
PT
1188/* If this inode has objects allocated to it (lsm != NULL), then the OST
1189 * object(s) determine the file size and mtime. Otherwise, the MDS will
1190 * keep these values until such a time that objects are allocated for it.
1191 * We do the MDS operations first, as it is checking permissions for us.
1192 * We don't to the MDS RPC if there is nothing that we want to store there,
1193 * otherwise there is no harm in updating mtime/atime on the MDS if we are
1194 * going to do an RPC anyways.
1195 *
1196 * If we are doing a truncate, we will send the mtime and ctime updates
1197 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1198 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1199 * at the same time.
a720b790
JL
1200 *
1201 * In case of HSMimport, we only set attr on MDS.
d7e09d03 1202 */
a720b790 1203int ll_setattr_raw(struct dentry *dentry, struct iattr *attr, bool hsm_import)
d7e09d03 1204{
2b0143b5 1205 struct inode *inode = d_inode(dentry);
d7e09d03
PT
1206 struct ll_inode_info *lli = ll_i2info(inode);
1207 struct md_op_data *op_data = NULL;
1208 struct md_open_data *mod = NULL;
5ea17d6c 1209 bool file_is_released = false;
d7e09d03 1210 int rc = 0, rc1 = 0;
d7e09d03 1211
a720b790 1212 CDEBUG(D_VFSTRACE,
e15ba45d
OD
1213 "%s: setattr inode %p/fid:" DFID
1214 " from %llu to %llu, valid %x, hsm_import %d\n",
1215 ll_get_fsname(inode->i_sb, NULL, 0), inode,
1216 PFID(&lli->lli_fid), i_size_read(inode), attr->ia_size,
1217 attr->ia_valid, hsm_import);
d7e09d03
PT
1218
1219 if (attr->ia_valid & ATTR_SIZE) {
1220 /* Check new size against VFS/VM file size limit and rlimit */
1221 rc = inode_newsize_ok(inode, attr->ia_size);
1222 if (rc)
0a3bdb00 1223 return rc;
d7e09d03
PT
1224
1225 /* The maximum Lustre file size is variable, based on the
1226 * OST maximum object size and number of stripes. This
c0894c6c
OD
1227 * needs another check in addition to the VFS check above.
1228 */
d7e09d03 1229 if (attr->ia_size > ll_file_maxbytes(inode)) {
1d8cb70c 1230 CDEBUG(D_INODE, "file "DFID" too large %llu > %llu\n",
d7e09d03
PT
1231 PFID(&lli->lli_fid), attr->ia_size,
1232 ll_file_maxbytes(inode));
0a3bdb00 1233 return -EFBIG;
d7e09d03
PT
1234 }
1235
1236 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1237 }
1238
1239 /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1240 if (attr->ia_valid & TIMES_SET_FLAGS) {
4b1a25f0 1241 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
2eb90a75 1242 !capable(CFS_CAP_FOWNER))
0a3bdb00 1243 return -EPERM;
d7e09d03
PT
1244 }
1245
1246 /* We mark all of the fields "set" so MDS/OST does not re-set them */
1247 if (attr->ia_valid & ATTR_CTIME) {
0f1c743b 1248 attr->ia_ctime = CURRENT_TIME;
d7e09d03
PT
1249 attr->ia_valid |= ATTR_CTIME_SET;
1250 }
1251 if (!(attr->ia_valid & ATTR_ATIME_SET) &&
1252 (attr->ia_valid & ATTR_ATIME)) {
0f1c743b 1253 attr->ia_atime = CURRENT_TIME;
d7e09d03
PT
1254 attr->ia_valid |= ATTR_ATIME_SET;
1255 }
1256 if (!(attr->ia_valid & ATTR_MTIME_SET) &&
1257 (attr->ia_valid & ATTR_MTIME)) {
0f1c743b 1258 attr->ia_mtime = CURRENT_TIME;
d7e09d03
PT
1259 attr->ia_valid |= ATTR_MTIME_SET;
1260 }
1261
1262 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
8d7eed54 1263 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %llu\n",
d7e09d03 1264 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
8d7eed54 1265 (s64)ktime_get_real_seconds());
d7e09d03
PT
1266
1267 /* If we are changing file size, file content is modified, flag it. */
1268 if (attr->ia_valid & ATTR_SIZE) {
1269 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1270 spin_lock(&lli->lli_lock);
1271 lli->lli_flags |= LLIF_DATA_MODIFIED;
1272 spin_unlock(&lli->lli_lock);
1273 }
1274
1275 /* We always do an MDS RPC, even if we're only changing the size;
c0894c6c
OD
1276 * only the MDS knows whether truncate() should fail with -ETXTBUSY
1277 */
d7e09d03 1278
496a51bd
JL
1279 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
1280 if (!op_data)
0a3bdb00 1281 return -ENOMEM;
d7e09d03 1282
81e053c7 1283 if (!S_ISDIR(inode->i_mode))
5955102c 1284 inode_unlock(inode);
d7e09d03
PT
1285
1286 memcpy(&op_data->op_attr, attr, sizeof(*attr));
1287
1288 /* Open epoch for truncate. */
1289 if (exp_connect_som(ll_i2mdexp(inode)) &&
1290 (attr->ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MTIME_SET)))
1291 op_data->op_flags = MF_EPOCH_OPEN;
1292
5ea17d6c
JL
1293 /* truncate on a released file must failed with -ENODATA,
1294 * so size must not be set on MDS for released file
1295 * but other attributes must be set
1296 */
1297 if (S_ISREG(inode->i_mode)) {
1298 struct lov_stripe_md *lsm;
1299 __u32 gen;
1300
1301 ll_layout_refresh(inode, &gen);
1302 lsm = ccc_inode_lsm_get(inode);
1303 if (lsm && lsm->lsm_pattern & LOV_PATTERN_F_RELEASED)
1304 file_is_released = true;
1305 ccc_inode_lsm_put(inode, lsm);
1306 }
1307
a720b790 1308 /* if not in HSM import mode, clear size attr for released file
5ea17d6c
JL
1309 * we clear the attribute send to MDT in op_data, not the original
1310 * received from caller in attr which is used later to
c0894c6c
OD
1311 * decide return code
1312 */
a720b790 1313 if (file_is_released && (attr->ia_valid & ATTR_SIZE) && !hsm_import)
5ea17d6c
JL
1314 op_data->op_attr.ia_valid &= ~ATTR_SIZE;
1315
d7e09d03
PT
1316 rc = ll_md_setattr(dentry, op_data, &mod);
1317 if (rc)
34e1f2bb 1318 goto out;
d7e09d03 1319
a720b790 1320 /* truncate failed (only when non HSM import), others succeed */
5ea17d6c 1321 if (file_is_released) {
a720b790 1322 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
34e1f2bb 1323 rc = -ENODATA;
5ea17d6c 1324 else
34e1f2bb
JL
1325 rc = 0;
1326 goto out;
5ea17d6c
JL
1327 }
1328
d7e09d03 1329 /* RPC to MDT is sent, cancel data modification flag */
5210a63a 1330 if (op_data->op_bias & MDS_DATA_MODIFIED) {
d7e09d03
PT
1331 spin_lock(&lli->lli_lock);
1332 lli->lli_flags &= ~LLIF_DATA_MODIFIED;
1333 spin_unlock(&lli->lli_lock);
1334 }
1335
1336 ll_ioepoch_open(lli, op_data->op_ioepoch);
34e1f2bb
JL
1337 if (!S_ISREG(inode->i_mode)) {
1338 rc = 0;
1339 goto out;
1340 }
d7e09d03
PT
1341
1342 if (attr->ia_valid & (ATTR_SIZE |
1343 ATTR_ATIME | ATTR_ATIME_SET |
53bd4a00 1344 ATTR_MTIME | ATTR_MTIME_SET)) {
d7e09d03
PT
1345 /* For truncate and utimes sending attributes to OSTs, setting
1346 * mtime/atime to the past will be performed under PW [0:EOF]
1347 * extent lock (new_size:EOF for truncate). It may seem
1348 * excessive to send mtime/atime updates to OSTs when not
1349 * setting times to past, but it is necessary due to possible
c0894c6c
OD
1350 * time de-synchronization between MDT inode and OST objects
1351 */
178ba1e0
BJ
1352 if (attr->ia_valid & ATTR_SIZE)
1353 down_write(&lli->lli_trunc_sem);
ef2e0f55 1354 rc = cl_setattr_ost(inode, attr);
178ba1e0
BJ
1355 if (attr->ia_valid & ATTR_SIZE)
1356 up_write(&lli->lli_trunc_sem);
53bd4a00 1357 }
d7e09d03 1358out:
83d6b8fe
SB
1359 if (op_data->op_ioepoch) {
1360 rc1 = ll_setattr_done_writing(inode, op_data, mod);
1361 if (!rc)
1362 rc = rc1;
d7e09d03 1363 }
83d6b8fe
SB
1364 ll_finish_md_op_data(op_data);
1365
d7e09d03 1366 if (!S_ISDIR(inode->i_mode)) {
5955102c 1367 inode_lock(inode);
a720b790 1368 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
d7e09d03
PT
1369 inode_dio_wait(inode);
1370 }
1371
1372 ll_stats_ops_tally(ll_i2sbi(inode), (attr->ia_valid & ATTR_SIZE) ?
1373 LPROC_LL_TRUNC : LPROC_LL_SETATTR, 1);
1374
251c4317 1375 return rc;
d7e09d03
PT
1376}
1377
1378int ll_setattr(struct dentry *de, struct iattr *attr)
1379{
2b0143b5 1380 int mode = d_inode(de)->i_mode;
d7e09d03
PT
1381
1382 if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1383 (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1384 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1385
1386 if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
1387 (ATTR_SIZE|ATTR_MODE)) &&
1388 (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
1389 (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1390 !(attr->ia_mode & S_ISGID))))
1391 attr->ia_valid |= ATTR_FORCE;
1392
98639249
NC
1393 if ((attr->ia_valid & ATTR_MODE) &&
1394 (mode & S_ISUID) &&
d7e09d03
PT
1395 !(attr->ia_mode & S_ISUID) &&
1396 !(attr->ia_valid & ATTR_KILL_SUID))
1397 attr->ia_valid |= ATTR_KILL_SUID;
1398
98639249
NC
1399 if ((attr->ia_valid & ATTR_MODE) &&
1400 ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
d7e09d03
PT
1401 !(attr->ia_mode & S_ISGID) &&
1402 !(attr->ia_valid & ATTR_KILL_SGID))
1403 attr->ia_valid |= ATTR_KILL_SGID;
1404
a720b790 1405 return ll_setattr_raw(de, attr, false);
d7e09d03
PT
1406}
1407
1408int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1409 __u64 max_age, __u32 flags)
1410{
1411 struct ll_sb_info *sbi = ll_s2sbi(sb);
1412 struct obd_statfs obd_osfs;
1413 int rc;
d7e09d03
PT
1414
1415 rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
1416 if (rc) {
1417 CERROR("md_statfs fails: rc = %d\n", rc);
0a3bdb00 1418 return rc;
d7e09d03
PT
1419 }
1420
1421 osfs->os_type = sb->s_magic;
1422
b0f5aad5 1423 CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
1d8cb70c
GD
1424 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,
1425 osfs->os_files);
d7e09d03
PT
1426
1427 if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
1428 flags |= OBD_STATFS_NODELAY;
1429
1430 rc = obd_statfs_rqset(sbi->ll_dt_exp, &obd_osfs, max_age, flags);
1431 if (rc) {
1432 CERROR("obd_statfs fails: rc = %d\n", rc);
0a3bdb00 1433 return rc;
d7e09d03
PT
1434 }
1435
b0f5aad5 1436 CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
d7e09d03
PT
1437 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1438 obd_osfs.os_files);
1439
1440 osfs->os_bsize = obd_osfs.os_bsize;
1441 osfs->os_blocks = obd_osfs.os_blocks;
1442 osfs->os_bfree = obd_osfs.os_bfree;
1443 osfs->os_bavail = obd_osfs.os_bavail;
1444
1445 /* If we don't have as many objects free on the OST as inodes
1446 * on the MDS, we reduce the total number of inodes to
1447 * compensate, so that the "inodes in use" number is correct.
1448 */
1449 if (obd_osfs.os_ffree < osfs->os_ffree) {
1450 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1451 obd_osfs.os_ffree;
1452 osfs->os_ffree = obd_osfs.os_ffree;
1453 }
1454
0a3bdb00 1455 return rc;
d7e09d03 1456}
c9f6bb96 1457
d7e09d03
PT
1458int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1459{
1460 struct super_block *sb = de->d_sb;
1461 struct obd_statfs osfs;
1462 int rc;
1463
b0f5aad5 1464 CDEBUG(D_VFSTRACE, "VFS Op: at %llu jiffies\n", get_jiffies_64());
d7e09d03
PT
1465 ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1466
1467 /* Some amount of caching on the client is allowed */
1468 rc = ll_statfs_internal(sb, &osfs,
1469 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1470 0);
1471 if (rc)
1472 return rc;
1473
1474 statfs_unpack(sfs, &osfs);
1475
1476 /* We need to downshift for all 32-bit kernels, because we can't
1477 * tell if the kernel is being called via sys_statfs64() or not.
1478 * Stop before overflowing f_bsize - in which case it is better
c0894c6c
OD
1479 * to just risk EOVERFLOW if caller is using old sys_statfs().
1480 */
d7e09d03
PT
1481 if (sizeof(long) < 8) {
1482 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1483 sfs->f_bsize <<= 1;
1484
1485 osfs.os_blocks >>= 1;
1486 osfs.os_bfree >>= 1;
1487 osfs.os_bavail >>= 1;
1488 }
1489 }
1490
1491 sfs->f_blocks = osfs.os_blocks;
1492 sfs->f_bfree = osfs.os_bfree;
1493 sfs->f_bavail = osfs.os_bavail;
bd994071 1494 sfs->f_fsid = ll_s2sbi(sb)->ll_fsid;
d7e09d03
PT
1495 return 0;
1496}
1497
1498void ll_inode_size_lock(struct inode *inode)
1499{
1500 struct ll_inode_info *lli;
1501
1502 LASSERT(!S_ISDIR(inode->i_mode));
1503
1504 lli = ll_i2info(inode);
47a57bde 1505 mutex_lock(&lli->lli_size_mutex);
d7e09d03
PT
1506}
1507
1508void ll_inode_size_unlock(struct inode *inode)
1509{
1510 struct ll_inode_info *lli;
1511
1512 lli = ll_i2info(inode);
47a57bde 1513 mutex_unlock(&lli->lli_size_mutex);
d7e09d03
PT
1514}
1515
1516void ll_update_inode(struct inode *inode, struct lustre_md *md)
1517{
1518 struct ll_inode_info *lli = ll_i2info(inode);
1519 struct mdt_body *body = md->body;
1520 struct lov_stripe_md *lsm = md->lsm;
1521 struct ll_sb_info *sbi = ll_i2sbi(inode);
1522
629ecb5b 1523 LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
6e16818b 1524 if (lsm) {
d7e09d03
PT
1525 if (!lli->lli_has_smd &&
1526 !(sbi->ll_flags & LL_SBI_LAYOUT_LOCK))
1527 cl_file_inode_init(inode, md);
1528
1529 lli->lli_maxbytes = lsm->lsm_maxbytes;
1530 if (lli->lli_maxbytes > MAX_LFS_FILESIZE)
1531 lli->lli_maxbytes = MAX_LFS_FILESIZE;
1532 }
1533
1534 if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
1535 if (body->valid & OBD_MD_FLRMTPERM)
1536 ll_update_remote_perm(inode, md->remote_perm);
1537 }
1538#ifdef CONFIG_FS_POSIX_ACL
1539 else if (body->valid & OBD_MD_FLACL) {
1540 spin_lock(&lli->lli_lock);
1541 if (lli->lli_posix_acl)
1542 posix_acl_release(lli->lli_posix_acl);
1543 lli->lli_posix_acl = md->posix_acl;
1544 spin_unlock(&lli->lli_lock);
1545 }
1546#endif
c1e2699d 1547 inode->i_ino = cl_fid_build_ino(&body->fid1,
1548 sbi->ll_flags & LL_SBI_32BIT_API);
d7e09d03
PT
1549 inode->i_generation = cl_fid_build_gen(&body->fid1);
1550
1551 if (body->valid & OBD_MD_FLATIME) {
1552 if (body->atime > LTIME_S(inode->i_atime))
1553 LTIME_S(inode->i_atime) = body->atime;
d2995737 1554 lli->lli_atime = body->atime;
d7e09d03
PT
1555 }
1556 if (body->valid & OBD_MD_FLMTIME) {
1557 if (body->mtime > LTIME_S(inode->i_mtime)) {
b0f5aad5
GKH
1558 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %llu\n",
1559 inode->i_ino, LTIME_S(inode->i_mtime),
1560 body->mtime);
d7e09d03
PT
1561 LTIME_S(inode->i_mtime) = body->mtime;
1562 }
d2995737 1563 lli->lli_mtime = body->mtime;
d7e09d03
PT
1564 }
1565 if (body->valid & OBD_MD_FLCTIME) {
1566 if (body->ctime > LTIME_S(inode->i_ctime))
1567 LTIME_S(inode->i_ctime) = body->ctime;
d2995737 1568 lli->lli_ctime = body->ctime;
d7e09d03
PT
1569 }
1570 if (body->valid & OBD_MD_FLMODE)
1571 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1572 if (body->valid & OBD_MD_FLTYPE)
1573 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1574 LASSERT(inode->i_mode != 0);
566be54d 1575 if (S_ISREG(inode->i_mode))
e6768831
TJ
1576 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS + 1,
1577 LL_MAX_BLKSIZE_BITS);
566be54d 1578 else
d7e09d03 1579 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
d7e09d03 1580 if (body->valid & OBD_MD_FLUID)
4b1a25f0 1581 inode->i_uid = make_kuid(&init_user_ns, body->uid);
d7e09d03 1582 if (body->valid & OBD_MD_FLGID)
4b1a25f0 1583 inode->i_gid = make_kgid(&init_user_ns, body->gid);
d7e09d03
PT
1584 if (body->valid & OBD_MD_FLFLAGS)
1585 inode->i_flags = ll_ext_to_inode_flags(body->flags);
1586 if (body->valid & OBD_MD_FLNLINK)
1587 set_nlink(inode, body->nlink);
1588 if (body->valid & OBD_MD_FLRDEV)
1589 inode->i_rdev = old_decode_dev(body->rdev);
1590
1591 if (body->valid & OBD_MD_FLID) {
1592 /* FID shouldn't be changed! */
1593 if (fid_is_sane(&lli->lli_fid)) {
1594 LASSERTF(lu_fid_eq(&lli->lli_fid, &body->fid1),
1595 "Trying to change FID "DFID
1596 " to the "DFID", inode %lu/%u(%p)\n",
1597 PFID(&lli->lli_fid), PFID(&body->fid1),
1598 inode->i_ino, inode->i_generation, inode);
1599 } else
1600 lli->lli_fid = body->fid1;
1601 }
1602
1603 LASSERT(fid_seq(&lli->lli_fid) != 0);
1604
1605 if (body->valid & OBD_MD_FLSIZE) {
1606 if (exp_connect_som(ll_i2mdexp(inode)) &&
1607 S_ISREG(inode->i_mode)) {
1608 struct lustre_handle lockh;
52ee0d20 1609 enum ldlm_mode mode;
d7e09d03
PT
1610
1611 /* As it is possible a blocking ast has been processed
1612 * by this time, we need to check there is an UPDATE
1613 * lock on the client and set LLIF_MDS_SIZE_LOCK holding
c0894c6c
OD
1614 * it.
1615 */
d7e09d03 1616 mode = ll_take_md_lock(inode, MDS_INODELOCK_UPDATE,
7fc1f831
AP
1617 &lockh, LDLM_FL_CBPENDING,
1618 LCK_CR | LCK_CW |
1619 LCK_PR | LCK_PW);
d7e09d03
PT
1620 if (mode) {
1621 if (lli->lli_flags & (LLIF_DONE_WRITING |
1622 LLIF_EPOCH_PENDING |
1623 LLIF_SOM_DIRTY)) {
2d00bd17 1624 CERROR("ino %lu flags %u still has size authority! do not trust the size got from MDS\n",
d7e09d03
PT
1625 inode->i_ino, lli->lli_flags);
1626 } else {
1627 /* Use old size assignment to avoid
c0894c6c
OD
1628 * deadlock bz14138 & bz14326
1629 */
d7e09d03 1630 i_size_write(inode, body->size);
ae5ef67b 1631 spin_lock(&lli->lli_lock);
d7e09d03 1632 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
ae5ef67b 1633 spin_unlock(&lli->lli_lock);
d7e09d03
PT
1634 }
1635 ldlm_lock_decref(&lockh, mode);
1636 }
1637 } else {
1638 /* Use old size assignment to avoid
c0894c6c
OD
1639 * deadlock bz14138 & bz14326
1640 */
d7e09d03
PT
1641 i_size_write(inode, body->size);
1642
1643 CDEBUG(D_VFSTRACE, "inode=%lu, updating i_size %llu\n",
1644 inode->i_ino, (unsigned long long)body->size);
1645 }
1646
1647 if (body->valid & OBD_MD_FLBLOCKS)
1648 inode->i_blocks = body->blocks;
1649 }
1650
5ea17d6c
JL
1651 if (body->valid & OBD_MD_TSTATE) {
1652 if (body->t_state & MS_RESTORE)
1653 lli->lli_flags |= LLIF_FILE_RESTORING;
1654 }
d7e09d03
PT
1655}
1656
1657void ll_read_inode2(struct inode *inode, void *opaque)
1658{
1659 struct lustre_md *md = opaque;
1660 struct ll_inode_info *lli = ll_i2info(inode);
d7e09d03
PT
1661
1662 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1663 PFID(&lli->lli_fid), inode);
1664
1665 LASSERT(!lli->lli_has_smd);
1666
1667 /* Core attributes from the MDS first. This is a new inode, and
1668 * the VFS doesn't zero times in the core inode so we have to do
1669 * it ourselves. They will be overwritten by either MDS or OST
c0894c6c
OD
1670 * attributes - we just need to make sure they aren't newer.
1671 */
d7e09d03
PT
1672 LTIME_S(inode->i_mtime) = 0;
1673 LTIME_S(inode->i_atime) = 0;
1674 LTIME_S(inode->i_ctime) = 0;
1675 inode->i_rdev = 0;
1676 ll_update_inode(inode, md);
1677
1678 /* OIDEBUG(inode); */
1679
d7e09d03
PT
1680 if (S_ISREG(inode->i_mode)) {
1681 struct ll_sb_info *sbi = ll_i2sbi(inode);
cf29a7b6 1682
d7e09d03
PT
1683 inode->i_op = &ll_file_inode_operations;
1684 inode->i_fop = sbi->ll_fop;
1685 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
d7e09d03
PT
1686 } else if (S_ISDIR(inode->i_mode)) {
1687 inode->i_op = &ll_dir_inode_operations;
1688 inode->i_fop = &ll_dir_operations;
d7e09d03
PT
1689 } else if (S_ISLNK(inode->i_mode)) {
1690 inode->i_op = &ll_fast_symlink_inode_operations;
d7e09d03
PT
1691 } else {
1692 inode->i_op = &ll_special_inode_operations;
1693
1694 init_special_inode(inode, inode->i_mode,
1695 inode->i_rdev);
d7e09d03
PT
1696 }
1697}
1698
1699void ll_delete_inode(struct inode *inode)
1700{
1929c433 1701 struct ll_inode_info *lli = ll_i2info(inode);
d7e09d03 1702
6e16818b 1703 if (S_ISREG(inode->i_mode) && lli->lli_clob)
d7e09d03 1704 /* discard all dirty pages before truncating them, required by
c0894c6c
OD
1705 * osc_extent implementation at LU-1030.
1706 */
65fb55d1
NY
1707 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF,
1708 CL_FSYNC_DISCARD, 1);
d7e09d03 1709
91b0abe3 1710 truncate_inode_pages_final(&inode->i_data);
d7e09d03
PT
1711
1712 /* Workaround for LU-118 */
1713 if (inode->i_data.nrpages) {
c4226c54
VT
1714 spin_lock_irq(&inode->i_data.tree_lock);
1715 spin_unlock_irq(&inode->i_data.tree_lock);
d7e09d03 1716 LASSERTF(inode->i_data.nrpages == 0,
2d00bd17 1717 "inode=%lu/%u(%p) nrpages=%lu, see http://jira.whamcloud.com/browse/LU-118\n",
d7e09d03
PT
1718 inode->i_ino, inode->i_generation, inode,
1719 inode->i_data.nrpages);
1720 }
1721 /* Workaround end */
1722
1723 ll_clear_inode(inode);
1724 clear_inode(inode);
d7e09d03
PT
1725}
1726
1727int ll_iocontrol(struct inode *inode, struct file *file,
1728 unsigned int cmd, unsigned long arg)
1729{
1730 struct ll_sb_info *sbi = ll_i2sbi(inode);
1731 struct ptlrpc_request *req = NULL;
1732 int rc, flags = 0;
d7e09d03 1733
a58a38ac 1734 switch (cmd) {
d7e09d03
PT
1735 case FSFILT_IOC_GETFLAGS: {
1736 struct mdt_body *body;
1737 struct md_op_data *op_data;
1738
1739 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
1740 0, 0, LUSTRE_OPC_ANY,
1741 NULL);
1742 if (IS_ERR(op_data))
0a3bdb00 1743 return PTR_ERR(op_data);
d7e09d03
PT
1744
1745 op_data->op_valid = OBD_MD_FLFLAGS;
1746 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
1747 ll_finish_md_op_data(op_data);
1748 if (rc) {
1749 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
0a3bdb00 1750 return -abs(rc);
d7e09d03
PT
1751 }
1752
1753 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1754
1755 flags = body->flags;
1756
1757 ptlrpc_req_finished(req);
1758
7ac5db21 1759 return put_user(flags, (int __user *)arg);
d7e09d03
PT
1760 }
1761 case FSFILT_IOC_SETFLAGS: {
1762 struct lov_stripe_md *lsm;
45efd655 1763 struct obd_info oinfo = { };
d7e09d03
PT
1764 struct md_op_data *op_data;
1765
7ac5db21 1766 if (get_user(flags, (int __user *)arg))
0a3bdb00 1767 return -EFAULT;
d7e09d03
PT
1768
1769 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1770 LUSTRE_OPC_ANY, NULL);
1771 if (IS_ERR(op_data))
0a3bdb00 1772 return PTR_ERR(op_data);
d7e09d03
PT
1773
1774 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags = flags;
1775 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
1776 rc = md_setattr(sbi->ll_md_exp, op_data,
1777 NULL, 0, NULL, 0, &req, NULL);
1778 ll_finish_md_op_data(op_data);
1779 ptlrpc_req_finished(req);
1780 if (rc)
0a3bdb00 1781 return rc;
d7e09d03
PT
1782
1783 inode->i_flags = ll_ext_to_inode_flags(flags);
1784
1785 lsm = ccc_inode_lsm_get(inode);
5dd16419
JX
1786 if (!lsm_has_objects(lsm)) {
1787 ccc_inode_lsm_put(inode, lsm);
0a3bdb00 1788 return 0;
5dd16419 1789 }
d7e09d03 1790
21068c46 1791 oinfo.oi_oa = kmem_cache_zalloc(obdo_cachep, GFP_NOFS);
d7e09d03
PT
1792 if (!oinfo.oi_oa) {
1793 ccc_inode_lsm_put(inode, lsm);
0a3bdb00 1794 return -ENOMEM;
d7e09d03
PT
1795 }
1796 oinfo.oi_md = lsm;
1797 oinfo.oi_oa->o_oi = lsm->lsm_oi;
1798 oinfo.oi_oa->o_flags = flags;
1799 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS |
1800 OBD_MD_FLGROUP;
d7e09d03
PT
1801 obdo_set_parent_fid(oinfo.oi_oa, &ll_i2info(inode)->lli_fid);
1802 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
2ba262fb 1803 kmem_cache_free(obdo_cachep, oinfo.oi_oa);
d7e09d03
PT
1804 ccc_inode_lsm_put(inode, lsm);
1805
1806 if (rc && rc != -EPERM && rc != -EACCES)
1807 CERROR("osc_setattr_async fails: rc = %d\n", rc);
1808
0a3bdb00 1809 return rc;
d7e09d03
PT
1810 }
1811 default:
0a3bdb00 1812 return -ENOSYS;
d7e09d03
PT
1813 }
1814
0a3bdb00 1815 return 0;
d7e09d03
PT
1816}
1817
1818int ll_flush_ctx(struct inode *inode)
1819{
1820 struct ll_sb_info *sbi = ll_i2sbi(inode);
1821
4b1a25f0 1822 CDEBUG(D_SEC, "flush context for user %d\n",
e15ba45d 1823 from_kuid(&init_user_ns, current_uid()));
d7e09d03
PT
1824
1825 obd_set_info_async(NULL, sbi->ll_md_exp,
1826 sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
1827 0, NULL, NULL);
1828 obd_set_info_async(NULL, sbi->ll_dt_exp,
1829 sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
1830 0, NULL, NULL);
1831 return 0;
1832}
1833
1834/* umount -f client means force down, don't save state */
1835void ll_umount_begin(struct super_block *sb)
1836{
1837 struct ll_sb_info *sbi = ll_s2sbi(sb);
1838 struct obd_device *obd;
1839 struct obd_ioctl_data *ioc_data;
d7e09d03
PT
1840
1841 CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1842 sb->s_count, atomic_read(&sb->s_active));
1843
1844 obd = class_exp2obd(sbi->ll_md_exp);
6e16818b 1845 if (!obd) {
55f5a824 1846 CERROR("Invalid MDC connection handle %#llx\n",
d7e09d03 1847 sbi->ll_md_exp->exp_handle.h_cookie);
d7e09d03
PT
1848 return;
1849 }
1850 obd->obd_force = 1;
1851
1852 obd = class_exp2obd(sbi->ll_dt_exp);
6e16818b 1853 if (!obd) {
55f5a824 1854 CERROR("Invalid LOV connection handle %#llx\n",
d7e09d03 1855 sbi->ll_dt_exp->exp_handle.h_cookie);
d7e09d03
PT
1856 return;
1857 }
1858 obd->obd_force = 1;
1859
496a51bd 1860 ioc_data = kzalloc(sizeof(*ioc_data), GFP_NOFS);
d7e09d03
PT
1861 if (ioc_data) {
1862 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
ec83e611 1863 sizeof(*ioc_data), ioc_data, NULL);
d7e09d03
PT
1864
1865 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
ec83e611 1866 sizeof(*ioc_data), ioc_data, NULL);
d7e09d03 1867
97903a26 1868 kfree(ioc_data);
d7e09d03
PT
1869 }
1870
d7e09d03
PT
1871 /* Really, we'd like to wait until there are no requests outstanding,
1872 * and then continue. For now, we just invalidate the requests,
1873 * schedule() and sleep one second if needed, and hope.
1874 */
1875 schedule();
d7e09d03
PT
1876}
1877
1878int ll_remount_fs(struct super_block *sb, int *flags, char *data)
1879{
1880 struct ll_sb_info *sbi = ll_s2sbi(sb);
1881 char *profilenm = get_profile_name(sb);
1882 int err;
1883 __u32 read_only;
1884
1885 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1886 read_only = *flags & MS_RDONLY;
1887 err = obd_set_info_async(NULL, sbi->ll_md_exp,
1888 sizeof(KEY_READ_ONLY),
1889 KEY_READ_ONLY, sizeof(read_only),
1890 &read_only, NULL);
1891 if (err) {
1892 LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
1893 profilenm, read_only ?
1894 "read-only" : "read-write", err);
1895 return err;
1896 }
1897
1898 if (read_only)
1899 sb->s_flags |= MS_RDONLY;
1900 else
1901 sb->s_flags &= ~MS_RDONLY;
1902
1903 if (sbi->ll_flags & LL_SBI_VERBOSE)
1904 LCONSOLE_WARN("Remounted %s %s\n", profilenm,
1905 read_only ? "read-only" : "read-write");
1906 }
1907 return 0;
1908}
1909
44ecac68
FY
1910/**
1911 * Cleanup the open handle that is cached on MDT-side.
1912 *
1913 * For open case, the client side open handling thread may hit error
1914 * after the MDT grant the open. Under such case, the client should
1915 * send close RPC to the MDT as cleanup; otherwise, the open handle
1916 * on the MDT will be leaked there until the client umount or evicted.
1917 *
1918 * In further, if someone unlinked the file, because the open handle
1919 * holds the reference on such file/object, then it will block the
1920 * subsequent threads that want to locate such object via FID.
1921 *
1922 * \param[in] sb super block for this file-system
1923 * \param[in] open_req pointer to the original open request
1924 */
1925void ll_open_cleanup(struct super_block *sb, struct ptlrpc_request *open_req)
1926{
1927 struct mdt_body *body;
1928 struct md_op_data *op_data;
1929 struct ptlrpc_request *close_req = NULL;
1930 struct obd_export *exp = ll_s2sbi(sb)->ll_md_exp;
1931
1932 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
af13af52 1933 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
83ea341d 1934 if (!op_data)
44ecac68 1935 return;
44ecac68
FY
1936
1937 op_data->op_fid1 = body->fid1;
1938 op_data->op_ioepoch = body->ioepoch;
1939 op_data->op_handle = body->handle;
1940 op_data->op_mod_time = get_seconds();
1941 md_close(exp, op_data, NULL, &close_req);
1942 ptlrpc_req_finished(close_req);
1943 ll_finish_md_op_data(op_data);
1944}
1945
d7e09d03
PT
1946int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
1947 struct super_block *sb, struct lookup_intent *it)
1948{
1949 struct ll_sb_info *sbi = NULL;
24af3e16 1950 struct lustre_md md = { NULL };
d7e09d03 1951 int rc;
d7e09d03
PT
1952
1953 LASSERT(*inode || sb);
1954 sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
1955 rc = md_get_lustre_md(sbi->ll_md_exp, req, sbi->ll_dt_exp,
1956 sbi->ll_md_exp, &md);
1957 if (rc)
44ecac68 1958 goto cleanup;
d7e09d03
PT
1959
1960 if (*inode) {
1961 ll_update_inode(*inode, &md);
1962 } else {
6e16818b 1963 LASSERT(sb);
d7e09d03
PT
1964
1965 /*
1966 * At this point server returns to client's same fid as client
1967 * generated for creating. So using ->fid1 is okay here.
1968 */
1969 LASSERT(fid_is_sane(&md.body->fid1));
1970
1971 *inode = ll_iget(sb, cl_fid_build_ino(&md.body->fid1,
c1e2699d 1972 sbi->ll_flags & LL_SBI_32BIT_API),
d7e09d03 1973 &md);
4ae51a74 1974 if (!*inode) {
d7e09d03
PT
1975#ifdef CONFIG_FS_POSIX_ACL
1976 if (md.posix_acl) {
1977 posix_acl_release(md.posix_acl);
1978 md.posix_acl = NULL;
1979 }
1980#endif
020ecc6f 1981 rc = -ENOMEM;
d7e09d03 1982 CERROR("new_inode -fatal: rc %d\n", rc);
34e1f2bb 1983 goto out;
d7e09d03
PT
1984 }
1985 }
1986
1987 /* Handling piggyback layout lock.
1988 * Layout lock can be piggybacked by getattr and open request.
1989 * The lsm can be applied to inode only if it comes with a layout lock
1990 * otherwise correct layout may be overwritten, for example:
1991 * 1. proc1: mdt returns a lsm but not granting layout
1992 * 2. layout was changed by another client
1993 * 3. proc2: refresh layout and layout lock granted
c0894c6c
OD
1994 * 4. proc1: to apply a stale layout
1995 */
6e16818b 1996 if (it && it->d.lustre.it_lock_mode != 0) {
d7e09d03
PT
1997 struct lustre_handle lockh;
1998 struct ldlm_lock *lock;
1999
2000 lockh.cookie = it->d.lustre.it_lock_handle;
2001 lock = ldlm_handle2lock(&lockh);
6e16818b 2002 LASSERT(lock);
d7e09d03
PT
2003 if (ldlm_has_layout(lock)) {
2004 struct cl_object_conf conf;
2005
2006 memset(&conf, 0, sizeof(conf));
2007 conf.coc_opc = OBJECT_CONF_SET;
2008 conf.coc_inode = *inode;
2009 conf.coc_lock = lock;
2010 conf.u.coc_md = &md;
2011 (void)ll_layout_conf(*inode, &conf);
2012 }
2013 LDLM_LOCK_PUT(lock);
2014 }
2015
2016out:
6e16818b 2017 if (md.lsm)
d7e09d03
PT
2018 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
2019 md_free_lustre_md(sbi->ll_md_exp, &md);
44ecac68
FY
2020
2021cleanup:
2022 if (rc != 0 && it && it->it_op & IT_OPEN)
2023 ll_open_cleanup(sb ? sb : (*inode)->i_sb, req);
2024
0a3bdb00 2025 return rc;
d7e09d03
PT
2026}
2027
4c6243ec 2028int ll_obd_statfs(struct inode *inode, void __user *arg)
d7e09d03
PT
2029{
2030 struct ll_sb_info *sbi = NULL;
2031 struct obd_export *exp;
2032 char *buf = NULL;
2033 struct obd_ioctl_data *data = NULL;
2034 __u32 type;
d7e09d03
PT
2035 int len = 0, rc;
2036
c650ba73
TR
2037 if (!inode) {
2038 rc = -EINVAL;
2039 goto out_statfs;
2040 }
2041
2042 sbi = ll_i2sbi(inode);
2043 if (!sbi) {
34e1f2bb
JL
2044 rc = -EINVAL;
2045 goto out_statfs;
2046 }
d7e09d03
PT
2047
2048 rc = obd_ioctl_getdata(&buf, &len, arg);
2049 if (rc)
34e1f2bb 2050 goto out_statfs;
d7e09d03 2051
bdbb0512 2052 data = (void *)buf;
d7e09d03 2053 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
34e1f2bb
JL
2054 !data->ioc_pbuf1 || !data->ioc_pbuf2) {
2055 rc = -EINVAL;
2056 goto out_statfs;
2057 }
d7e09d03
PT
2058
2059 if (data->ioc_inllen1 != sizeof(__u32) ||
2060 data->ioc_inllen2 != sizeof(__u32) ||
2061 data->ioc_plen1 != sizeof(struct obd_statfs) ||
34e1f2bb
JL
2062 data->ioc_plen2 != sizeof(struct obd_uuid)) {
2063 rc = -EINVAL;
2064 goto out_statfs;
2065 }
d7e09d03
PT
2066
2067 memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2068 if (type & LL_STATFS_LMV)
2069 exp = sbi->ll_md_exp;
2070 else if (type & LL_STATFS_LOV)
2071 exp = sbi->ll_dt_exp;
34e1f2bb
JL
2072 else {
2073 rc = -ENODEV;
2074 goto out_statfs;
2075 }
d7e09d03 2076
44164fc9 2077 rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, buf, NULL);
d7e09d03 2078 if (rc)
34e1f2bb 2079 goto out_statfs;
d7e09d03
PT
2080out_statfs:
2081 if (buf)
2082 obd_ioctl_freedata(buf, len);
2083 return rc;
2084}
2085
2086int ll_process_config(struct lustre_cfg *lcfg)
2087{
2088 char *ptr;
2089 void *sb;
2090 struct lprocfs_static_vars lvars;
2091 unsigned long x;
2092 int rc = 0;
2093
2094 lprocfs_llite_init_vars(&lvars);
2095
2096 /* The instance name contains the sb: lustre-client-aacfe000 */
2097 ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2098 if (!ptr || !*(++ptr))
2099 return -EINVAL;
692f2b6c 2100 rc = kstrtoul(ptr, 16, &x);
2101 if (rc != 0)
d7e09d03
PT
2102 return -EINVAL;
2103 sb = (void *)x;
2104 /* This better be a real Lustre superblock! */
2105 LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2106
2107 /* Note we have not called client_common_fill_super yet, so
c0894c6c
OD
2108 * proc fns must be able to handle that!
2109 */
d7e09d03
PT
2110 rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2111 lcfg, sb);
2112 if (rc > 0)
2113 rc = 0;
fbe7c6c7 2114 return rc;
d7e09d03
PT
2115}
2116
2117/* this function prepares md_op_data hint for passing ot down to MD stack. */
aff9d8e8 2118struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
e15ba45d
OD
2119 struct inode *i1, struct inode *i2,
2120 const char *name, int namelen,
2121 int mode, __u32 opc, void *data)
d7e09d03 2122{
d7e09d03
PT
2123 if (namelen > ll_i2sbi(i1)->ll_namelen)
2124 return ERR_PTR(-ENAMETOOLONG);
2125
6e16818b 2126 if (!op_data)
496a51bd 2127 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
d7e09d03 2128
6e16818b 2129 if (!op_data)
d7e09d03
PT
2130 return ERR_PTR(-ENOMEM);
2131
2132 ll_i2gids(op_data->op_suppgids, i1, i2);
2133 op_data->op_fid1 = *ll_inode2fid(i1);
d7e09d03 2134
ef2e0f55 2135 if (i2)
d7e09d03 2136 op_data->op_fid2 = *ll_inode2fid(i2);
ef2e0f55 2137 else
d7e09d03 2138 fid_zero(&op_data->op_fid2);
d7e09d03
PT
2139
2140 op_data->op_name = name;
2141 op_data->op_namelen = namelen;
2142 op_data->op_mode = mode;
14e3f92a 2143 op_data->op_mod_time = ktime_get_real_seconds();
4b1a25f0
PT
2144 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2145 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
d7e09d03
PT
2146 op_data->op_cap = cfs_curproc_cap_pack();
2147 op_data->op_bias = 0;
2148 op_data->op_cli_flags = 0;
6e16818b
OD
2149 if ((opc == LUSTRE_OPC_CREATE) && name &&
2150 filename_is_volatile(name, namelen, NULL))
d7e09d03
PT
2151 op_data->op_bias |= MDS_CREATE_VOLATILE;
2152 op_data->op_opc = opc;
2153 op_data->op_mds = 0;
2154 op_data->op_data = data;
2155
2156 /* If the file is being opened after mknod() (normally due to NFS)
2157 * try to use the default stripe data from parent directory for
c0894c6c
OD
2158 * allocating OST objects. Try to pass the parent FID to MDS.
2159 */
d7e09d03
PT
2160 if (opc == LUSTRE_OPC_CREATE && i1 == i2 && S_ISREG(i2->i_mode) &&
2161 !ll_i2info(i2)->lli_has_smd) {
2162 struct ll_inode_info *lli = ll_i2info(i2);
2163
2164 spin_lock(&lli->lli_lock);
2165 if (likely(!lli->lli_has_smd && !fid_is_zero(&lli->lli_pfid)))
2166 op_data->op_fid1 = lli->lli_pfid;
2167 spin_unlock(&lli->lli_lock);
d7e09d03
PT
2168 }
2169
2170 /* When called by ll_setattr_raw, file is i1. */
1f6eaf83 2171 if (ll_i2info(i1)->lli_flags & LLIF_DATA_MODIFIED)
d7e09d03
PT
2172 op_data->op_bias |= MDS_DATA_MODIFIED;
2173
2174 return op_data;
2175}
2176
2177void ll_finish_md_op_data(struct md_op_data *op_data)
2178{
97903a26 2179 kfree(op_data);
d7e09d03
PT
2180}
2181
2182int ll_show_options(struct seq_file *seq, struct dentry *dentry)
2183{
2184 struct ll_sb_info *sbi;
2185
6e16818b 2186 LASSERT(seq && dentry);
d7e09d03
PT
2187 sbi = ll_s2sbi(dentry->d_sb);
2188
2189 if (sbi->ll_flags & LL_SBI_NOLCK)
2190 seq_puts(seq, ",nolock");
2191
2192 if (sbi->ll_flags & LL_SBI_FLOCK)
2193 seq_puts(seq, ",flock");
2194
2195 if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2196 seq_puts(seq, ",localflock");
2197
2198 if (sbi->ll_flags & LL_SBI_USER_XATTR)
2199 seq_puts(seq, ",user_xattr");
2200
2201 if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
2202 seq_puts(seq, ",lazystatfs");
2203
2204 if (sbi->ll_flags & LL_SBI_USER_FID2PATH)
2205 seq_puts(seq, ",user_fid2path");
2206
0a3bdb00 2207 return 0;
d7e09d03
PT
2208}
2209
2210/**
2211 * Get obd name by cmd, and copy out to user space
2212 */
2213int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg)
2214{
2215 struct ll_sb_info *sbi = ll_i2sbi(inode);
2216 struct obd_device *obd;
d7e09d03
PT
2217
2218 if (cmd == OBD_IOC_GETDTNAME)
2219 obd = class_exp2obd(sbi->ll_dt_exp);
2220 else if (cmd == OBD_IOC_GETMDNAME)
2221 obd = class_exp2obd(sbi->ll_md_exp);
2222 else
0a3bdb00 2223 return -EINVAL;
d7e09d03
PT
2224
2225 if (!obd)
0a3bdb00 2226 return -ENOENT;
d7e09d03 2227
7ac5db21
OD
2228 if (copy_to_user((void __user *)arg, obd->obd_name,
2229 strlen(obd->obd_name) + 1))
0a3bdb00 2230 return -EFAULT;
d7e09d03 2231
0a3bdb00 2232 return 0;
d7e09d03
PT
2233}
2234
2235/**
2236 * Get lustre file system name by \a sbi. If \a buf is provided(non-NULL), the
2237 * fsname will be returned in this buffer; otherwise, a static buffer will be
2238 * used to store the fsname and returned to caller.
2239 */
2240char *ll_get_fsname(struct super_block *sb, char *buf, int buflen)
2241{
2242 static char fsname_static[MTI_NAME_MAXLEN];
2243 struct lustre_sb_info *lsi = s2lsi(sb);
2244 char *ptr;
2245 int len;
2246
6e16818b 2247 if (!buf) {
d7e09d03
PT
2248 /* this means the caller wants to use static buffer
2249 * and it doesn't care about race. Usually this is
c0894c6c
OD
2250 * in error reporting path
2251 */
d7e09d03
PT
2252 buf = fsname_static;
2253 buflen = sizeof(fsname_static);
2254 }
2255
2256 len = strlen(lsi->lsi_lmd->lmd_profile);
2257 ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
2258 if (ptr && (strcmp(ptr, "-client") == 0))
2259 len -= 7;
2260
2261 if (unlikely(len >= buflen))
2262 len = buflen - 1;
2263 strncpy(buf, lsi->lsi_lmd->lmd_profile, len);
2264 buf[len] = '\0';
2265
2266 return buf;
2267}
2268
d7e09d03
PT
2269void ll_dirty_page_discard_warn(struct page *page, int ioret)
2270{
2271 char *buf, *path = NULL;
2272 struct dentry *dentry = NULL;
2273 struct ccc_object *obj = cl_inode2ccc(page->mapping->host);
2274
2275 /* this can be called inside spin lock so use GFP_ATOMIC. */
2276 buf = (char *)__get_free_page(GFP_ATOMIC);
6e16818b 2277 if (buf) {
d7e09d03 2278 dentry = d_find_alias(page->mapping->host);
6e16818b 2279 if (dentry)
1ad581eb 2280 path = dentry_path_raw(dentry, buf, PAGE_SIZE);
d7e09d03
PT
2281 }
2282
73b89907 2283 CDEBUG(D_WARNING,
2d00bd17
JP
2284 "%s: dirty page discard: %s/fid: " DFID "/%s may get corrupted (rc %d)\n",
2285 ll_get_fsname(page->mapping->host->i_sb, NULL, 0),
73b89907
RH
2286 s2lsi(page->mapping->host->i_sb)->lsi_lmd->lmd_dev,
2287 PFID(&obj->cob_header.coh_lu.loh_fid),
2288 (path && !IS_ERR(path)) ? path : "", ioret);
d7e09d03 2289
6e16818b 2290 if (dentry)
d7e09d03
PT
2291 dput(dentry);
2292
6e16818b 2293 if (buf)
d7e09d03
PT
2294 free_page((unsigned long)buf);
2295}