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