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