Merge remote-tracking branch 'staging/staging-next'
[deliverable/linux.git] / drivers / staging / lustre / lustre / llite / dir.c
CommitLineData
d7e09d03
PT
1/*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
6a5b99a4 18 * http://www.gnu.org/licenses/gpl-2.0.html
d7e09d03 19 *
d7e09d03
PT
20 * GPL HEADER END
21 */
22/*
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
1dc563a6 26 * Copyright (c) 2011, 2015, Intel Corporation.
d7e09d03
PT
27 */
28/*
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
31 *
32 * lustre/llite/dir.c
33 *
34 * Directory code for lustre client.
35 */
36
37#include <linux/fs.h>
38#include <linux/pagemap.h>
39#include <linux/mm.h>
31982482 40#include <linux/uaccess.h>
995c8b4a 41#include <linux/buffer_head.h> /* for wait_on_buffer */
d7e09d03 42#include <linux/pagevec.h>
2870cd10 43#include <linux/prefetch.h>
d7e09d03
PT
44
45#define DEBUG_SUBSYSTEM S_LLITE
46
67a235f5
GKH
47#include "../include/obd_support.h"
48#include "../include/obd_class.h"
8877d3bf 49#include "../include/lustre/lustre_ioctl.h"
67a235f5 50#include "../include/lustre_lib.h"
67a235f5
GKH
51#include "../include/lustre_lite.h"
52#include "../include/lustre_dlm.h"
53#include "../include/lustre_fid.h"
e2780478 54#include "../include/lustre_kernelcomm.h"
d7e09d03
PT
55#include "llite_internal.h"
56
57/*
58 * (new) readdir implementation overview.
59 *
60 * Original lustre readdir implementation cached exact copy of raw directory
61 * pages on the client. These pages were indexed in client page cache by
62 * logical offset in the directory file. This design, while very simple and
63 * intuitive had some inherent problems:
64 *
65 * . it implies that byte offset to the directory entry serves as a
66 * telldir(3)/seekdir(3) cookie, but that offset is not stable: in
67 * ext3/htree directory entries may move due to splits, and more
68 * importantly,
69 *
70 * . it is incompatible with the design of split directories for cmd3,
71 * that assumes that names are distributed across nodes based on their
72 * hash, and so readdir should be done in hash order.
73 *
74 * New readdir implementation does readdir in hash order, and uses hash of a
75 * file name as a telldir/seekdir cookie. This led to number of complications:
76 *
77 * . hash is not unique, so it cannot be used to index cached directory
78 * pages on the client (note, that it requires a whole pageful of hash
79 * collided entries to cause two pages to have identical hashes);
80 *
81 * . hash is not unique, so it cannot, strictly speaking, be used as an
82 * entry cookie. ext3/htree has the same problem and lustre implementation
83 * mimics their solution: seekdir(hash) positions directory at the first
84 * entry with the given hash.
85 *
86 * Client side.
87 *
88 * 0. caching
89 *
90 * Client caches directory pages using hash of the first entry as an index. As
91 * noted above hash is not unique, so this solution doesn't work as is:
92 * special processing is needed for "page hash chains" (i.e., sequences of
93 * pages filled with entries all having the same hash value).
94 *
95 * First, such chains have to be detected. To this end, server returns to the
96 * client the hash of the first entry on the page next to one returned. When
97 * client detects that this hash is the same as hash of the first entry on the
98 * returned page, page hash collision has to be handled. Pages in the
99 * hash chain, except first one, are termed "overflow pages".
100 *
101 * Solution to index uniqueness problem is to not cache overflow
102 * pages. Instead, when page hash collision is detected, all overflow pages
103 * from emerging chain are immediately requested from the server and placed in
104 * a special data structure (struct ll_dir_chain). This data structure is used
105 * by ll_readdir() to process entries from overflow pages. When readdir
106 * invocation finishes, overflow pages are discarded. If page hash collision
107 * chain weren't completely processed, next call to readdir will again detect
108 * page hash collision, again read overflow pages in, process next portion of
109 * entries and again discard the pages. This is not as wasteful as it looks,
110 * because, given reasonable hash, page hash collisions are extremely rare.
111 *
112 * 1. directory positioning
113 *
114 * When seekdir(hash) is called, original
115 *
116 *
117 *
118 *
119 *
120 *
121 *
122 *
123 * Server.
124 *
125 * identification of and access to overflow pages
126 *
127 * page format
128 *
129 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
130 * a header lu_dirpage which describes the start/end hash, and whether this
131 * page is empty (contains no dir entry) or hash collide with next page.
132 * After client receives reply, several pages will be integrated into dir page
ea1754a0
KS
133 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the lu_dirpage
134 * for this integrated page will be adjusted. See lmv_adjust_dirpages().
d7e09d03
PT
135 *
136 */
4f76f0ec 137struct page *ll_get_dir_page(struct inode *dir, struct md_op_data *op_data,
138 __u64 offset, struct ll_dir_chain *chain)
d7e09d03 139{
4f76f0ec 140 struct md_callback cb_op;
d7e09d03 141 struct page *page;
d7e09d03 142 int rc;
d7e09d03 143
4f76f0ec 144 cb_op.md_blocking_ast = ll_md_blocking_ast;
145 rc = md_read_page(ll_i2mdexp(dir), op_data, &cb_op, offset, &page);
146 if (rc)
147 return ERR_PTR(rc);
d7e09d03 148
4f76f0ec 149 return page;
d7e09d03
PT
150}
151
77a782ab 152void ll_release_page(struct inode *inode, struct page *page, bool remove)
d7e09d03
PT
153{
154 kunmap(page);
4f76f0ec 155
156 /*
157 * Always remove the page for striped dir, because the page is
158 * built from temporarily in LMV layer
159 */
160 if (inode && S_ISDIR(inode->i_mode) &&
161 ll_i2info(inode)->lli_lsm_md) {
162 __free_page(page);
163 return;
164 }
165
d7e09d03
PT
166 if (remove) {
167 lock_page(page);
6e16818b 168 if (likely(page->mapping))
d7e09d03
PT
169 truncate_complete_page(page->mapping, page);
170 unlock_page(page);
171 }
09cbfeaf 172 put_page(page);
d7e09d03
PT
173}
174
0097dcab
OD
175/**
176 * return IF_* type for given lu_dirent entry.
177 * IF_* flag shld be converted to particular OS file type in
178 * platform llite module.
179 */
180static __u16 ll_dirent_type_get(struct lu_dirent *ent)
181{
182 __u16 type = 0;
183 struct luda_type *lt;
184 int len = 0;
185
186 if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
187 const unsigned int align = sizeof(struct luda_type) - 1;
188
189 len = le16_to_cpu(ent->lde_namelen);
190 len = (len + align) & ~align;
191 lt = (void *)ent->lde_name + len;
192 type = IFTODT(le16_to_cpu(lt->lt_type));
193 }
194 return type;
195}
196
218ba485 197int ll_dir_read(struct inode *inode, __u64 *ppos, struct md_op_data *op_data,
307bef74 198 struct dir_context *ctx)
d7e09d03 199{
d7e09d03 200 struct ll_sb_info *sbi = ll_i2sbi(inode);
218ba485 201 __u64 pos = *ppos;
67537558 202 int is_api32 = ll_need_32bit_api(sbi);
203 int is_hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
d7e09d03
PT
204 struct page *page;
205 struct ll_dir_chain chain;
13810b0f 206 bool done = false;
d7e09d03 207 int rc = 0;
d7e09d03
PT
208
209 ll_dir_chain_init(&chain);
210
3978732f 211 page = ll_get_dir_page(inode, op_data, pos, &chain);
d7e09d03
PT
212
213 while (rc == 0 && !done) {
214 struct lu_dirpage *dp;
215 struct lu_dirent *ent;
b1f048c1 216 __u64 hash;
217 __u64 next;
218
219 if (IS_ERR(page)) {
220 rc = PTR_ERR(page);
221 break;
222 }
223
224 hash = MDS_DIR_END_OFF;
225 dp = page_address(page);
226 for (ent = lu_dirent_start(dp); ent && !done;
227 ent = lu_dirent_next(ent)) {
228 __u16 type;
229 int namelen;
230 struct lu_fid fid;
231 __u64 lhash;
232 __u64 ino;
d7e09d03 233
b1f048c1 234 hash = le64_to_cpu(ent->lde_hash);
235 if (hash < pos)
d7e09d03 236 /*
b1f048c1 237 * Skip until we find target hash
238 * value.
d7e09d03 239 */
b1f048c1 240 continue;
d7e09d03 241
b1f048c1 242 namelen = le16_to_cpu(ent->lde_namelen);
243 if (namelen == 0)
244 /*
245 * Skip dummy record.
0b09d381 246 */
b1f048c1 247 continue;
248
67537558 249 if (is_api32 && is_hash64)
b1f048c1 250 lhash = hash >> 32;
251 else
252 lhash = hash;
253 fid_le_to_cpu(&fid, &ent->lde_fid);
67537558 254 ino = cl_fid_build_ino(&fid, is_api32);
b1f048c1 255 type = ll_dirent_type_get(ent);
256 ctx->pos = lhash;
257 /* For 'll_nfs_get_name_filldir()', it will try
258 * to access the 'ent' through its 'lde_name',
259 * so the parameter 'name' for 'ctx->actor()'
260 * must be part of the 'ent'.
261 */
262 done = !dir_emit(ctx, ent->lde_name,
263 namelen, ino, type);
264 }
fb659ca1 265
fee8cc87 266 if (done) {
b1f048c1 267 pos = hash;
77a782ab 268 ll_release_page(inode, page, false);
fee8cc87 269 break;
270 }
271
272 next = le64_to_cpu(dp->ldp_hash_end);
273 pos = next;
274 if (pos == MDS_DIR_END_OFF) {
275 /*
276 * End of directory reached.
277 */
278 done = 1;
77a782ab 279 ll_release_page(inode, page, false);
26f5c084 280 } else {
fee8cc87 281 /*
282 * Normal case: continue to the next
283 * page.
284 */
006e4dcd 285 ll_release_page(inode, page,
fee8cc87 286 le32_to_cpu(dp->ldp_flags) &
287 LDF_COLLIDE);
288 next = pos;
3978732f 289 page = ll_get_dir_page(inode, op_data, pos,
fee8cc87 290 &chain);
d7e09d03
PT
291 }
292 }
293
0b09d381 294 ctx->pos = pos;
d7e09d03 295 ll_dir_chain_fini(&chain);
0a3bdb00 296 return rc;
d7e09d03
PT
297}
298
0b09d381 299static int ll_readdir(struct file *filp, struct dir_context *ctx)
d7e09d03 300{
2a8a3597 301 struct inode *inode = file_inode(filp);
d7e09d03
PT
302 struct ll_file_data *lfd = LUSTRE_FPRIVATE(filp);
303 struct ll_sb_info *sbi = ll_i2sbi(inode);
0fcd869f 304 __u64 pos = lfd ? lfd->lfd_pos : 0;
d7e09d03
PT
305 int hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
306 int api32 = ll_need_32bit_api(sbi);
307bef74 307 struct md_op_data *op_data;
d7e09d03 308 int rc;
d7e09d03 309
969d25d4 310 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) pos/size %lu/%llu 32bit_api %d\n",
97a075cd
JN
311 PFID(ll_inode2fid(inode)), inode, (unsigned long)pos,
312 i_size_read(inode), api32);
d7e09d03 313
0fcd869f 314 if (pos == MDS_DIR_END_OFF) {
d7e09d03
PT
315 /*
316 * end-of-file.
317 */
34e1f2bb
JL
318 rc = 0;
319 goto out;
320 }
d7e09d03 321
307bef74 322 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
323 LUSTRE_OPC_ANY, inode);
324 if (IS_ERR(op_data)) {
325 rc = PTR_ERR(op_data);
326 goto out;
327 }
328
ef21b1fb 329 if (unlikely(op_data->op_mea1)) {
330 /*
331 * This is only needed for striped dir to fill ..,
332 * see lmv_read_page
333 */
334 if (file_dentry(filp)->d_parent &&
335 file_dentry(filp)->d_parent->d_inode) {
336 __u64 ibits = MDS_INODELOCK_UPDATE;
337 struct inode *parent;
338
339 parent = file_dentry(filp)->d_parent->d_inode;
340 if (ll_have_md_lock(parent, &ibits, LCK_MINMODE))
341 op_data->op_fid3 = *ll_inode2fid(parent);
342 }
343
344 /*
345 * If it can not find in cache, do lookup .. on the master
346 * object
347 */
348 if (fid_is_zero(&op_data->op_fid3)) {
349 rc = ll_dir_get_parent_fid(inode, &op_data->op_fid3);
350 if (rc) {
351 ll_finish_md_op_data(op_data);
352 return rc;
353 }
354 }
355 }
bce1bbf4 356 op_data->op_max_pages = sbi->ll_md_brw_pages;
0fcd869f 357 ctx->pos = pos;
218ba485 358 rc = ll_dir_read(inode, &pos, op_data, ctx);
359 pos = ctx->pos;
0fcd869f 360 if (lfd)
218ba485 361 lfd->lfd_pos = pos;
362
363 if (pos == MDS_DIR_END_OFF) {
d7e09d03 364 if (api32)
218ba485 365 pos = LL_DIR_END_OFF_32BIT;
d7e09d03 366 else
218ba485 367 pos = LL_DIR_END_OFF;
d7e09d03
PT
368 } else {
369 if (api32 && hash64)
218ba485 370 pos >>= 32;
d7e09d03 371 }
218ba485 372 ctx->pos = pos;
307bef74 373 ll_finish_md_op_data(op_data);
d7e09d03 374 filp->f_version = inode->i_version;
d7e09d03
PT
375
376out:
377 if (!rc)
378 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
379
0a3bdb00 380 return rc;
d7e09d03
PT
381}
382
2d95f10e 383static int ll_send_mgc_param(struct obd_export *mgc, char *string)
d7e09d03
PT
384{
385 struct mgs_send_param *msp;
386 int rc = 0;
387
496a51bd 388 msp = kzalloc(sizeof(*msp), GFP_NOFS);
d7e09d03
PT
389 if (!msp)
390 return -ENOMEM;
391
9563fe8a 392 strlcpy(msp->mgs_param, string, sizeof(msp->mgs_param));
d7e09d03
PT
393 rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
394 sizeof(struct mgs_send_param), msp, NULL);
395 if (rc)
396 CERROR("Failed to set parameter: %d\n", rc);
97903a26 397 kfree(msp);
d7e09d03
PT
398
399 return rc;
400}
401
d07d4cb8 402/**
403 * Create striped directory with specified stripe(@lump)
404 *
405 * param[in] parent the parent of the directory.
406 * param[in] lump the specified stripes.
407 * param[in] dirname the name of the directory.
408 * param[in] mode the specified mode of the directory.
409 *
410 * retval =0 if striped directory is being created successfully.
411 * <0 if the creation is failed.
412 */
413static int ll_dir_setdirstripe(struct inode *parent, struct lmv_user_md *lump,
414 const char *dirname, umode_t mode)
d7e09d03
PT
415{
416 struct ptlrpc_request *request = NULL;
417 struct md_op_data *op_data;
d07d4cb8 418 struct ll_sb_info *sbi = ll_i2sbi(parent);
d7e09d03
PT
419 int err;
420
2de35386 421 if (unlikely(lump->lum_magic != LMV_USER_MAGIC))
422 return -EINVAL;
423
2de35386 424 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) name %s stripe_offset %d, stripe_count: %u\n",
d07d4cb8 425 PFID(ll_inode2fid(parent)), parent, dirname,
2de35386 426 (int)lump->lum_stripe_offset, lump->lum_stripe_count);
427
428 if (lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
429 lustre_swab_lmv_user_md(lump);
430
d07d4cb8 431 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
432 mode &= ~current_umask();
433 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
434 op_data = ll_prep_md_op_data(NULL, parent, NULL, dirname,
435 strlen(dirname), mode, LUSTRE_OPC_MKDIR,
d7e09d03 436 lump);
34e1f2bb
JL
437 if (IS_ERR(op_data)) {
438 err = PTR_ERR(op_data);
439 goto err_exit;
440 }
d7e09d03
PT
441
442 op_data->op_cli_flags |= CLI_SET_MEA;
443 err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
4b1a25f0
PT
444 from_kuid(&init_user_ns, current_fsuid()),
445 from_kgid(&init_user_ns, current_fsgid()),
d7e09d03
PT
446 cfs_curproc_cap_pack(), 0, &request);
447 ll_finish_md_op_data(op_data);
448 if (err)
34e1f2bb 449 goto err_exit;
d7e09d03
PT
450err_exit:
451 ptlrpc_req_finished(request);
452 return err;
453}
454
455int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
456 int set_default)
457{
458 struct ll_sb_info *sbi = ll_i2sbi(inode);
459 struct md_op_data *op_data;
460 struct ptlrpc_request *req = NULL;
461 int rc = 0;
462 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
463 struct obd_device *mgc = lsi->lsi_mgc;
464 int lum_size;
d7e09d03 465
6e16818b 466 if (lump) {
d7e09d03
PT
467 /*
468 * This is coming from userspace, so should be in
469 * local endian. But the MDS would like it in little
470 * endian, so we swab it before we send it.
471 */
472 switch (lump->lmm_magic) {
473 case LOV_USER_MAGIC_V1: {
474 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
475 lustre_swab_lov_user_md_v1(lump);
476 lum_size = sizeof(struct lov_user_md_v1);
477 break;
478 }
479 case LOV_USER_MAGIC_V3: {
480 if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
481 lustre_swab_lov_user_md_v3(
482 (struct lov_user_md_v3 *)lump);
483 lum_size = sizeof(struct lov_user_md_v3);
484 break;
485 }
6e23ea98 486 case LMV_USER_MAGIC: {
487 if (lump->lmm_magic != cpu_to_le32(LMV_USER_MAGIC))
488 lustre_swab_lmv_user_md(
489 (struct lmv_user_md *)lump);
490 lum_size = sizeof(struct lmv_user_md);
491 break;
492 }
d7e09d03 493 default: {
2d00bd17
JP
494 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
495 lump->lmm_magic, LOV_USER_MAGIC_V1,
496 LOV_USER_MAGIC_V3);
0a3bdb00 497 return -EINVAL;
d7e09d03
PT
498 }
499 }
500 } else {
501 lum_size = sizeof(struct lov_user_md_v1);
502 }
503
504 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
505 LUSTRE_OPC_ANY, NULL);
506 if (IS_ERR(op_data))
0a3bdb00 507 return PTR_ERR(op_data);
d7e09d03 508
d7e09d03
PT
509 /* swabbing is done in lov_setstripe() on server side */
510 rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
511 NULL, 0, &req, NULL);
512 ll_finish_md_op_data(op_data);
513 ptlrpc_req_finished(req);
514 if (rc) {
515 if (rc != -EPERM && rc != -EACCES)
516 CERROR("mdc_setattr fails: rc = %d\n", rc);
517 }
518
519 /* In the following we use the fact that LOV_USER_MAGIC_V1 and
c0894c6c
OD
520 * LOV_USER_MAGIC_V3 have the same initial fields so we do not
521 * need to make the distinction between the 2 versions
522 */
d7e09d03
PT
523 if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
524 char *param = NULL;
525 char *buf;
526
496a51bd 527 param = kzalloc(MGS_PARAM_MAXLEN, GFP_NOFS);
57876fd8
JL
528 if (!param)
529 return -ENOMEM;
d7e09d03
PT
530
531 buf = param;
532 /* Get fsname and assume devname to be -MDT0000. */
533 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
534 strcat(buf, "-MDT0000.lov");
535 buf += strlen(buf);
536
537 /* Set root stripesize */
538 sprintf(buf, ".stripesize=%u",
539 lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
540 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
541 if (rc)
34e1f2bb 542 goto end;
d7e09d03
PT
543
544 /* Set root stripecount */
545 sprintf(buf, ".stripecount=%hd",
546 lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
547 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
548 if (rc)
34e1f2bb 549 goto end;
d7e09d03
PT
550
551 /* Set root stripeoffset */
552 sprintf(buf, ".stripeoffset=%hd",
553 lump ? le16_to_cpu(lump->lmm_stripe_offset) :
554 (typeof(lump->lmm_stripe_offset))(-1));
555 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
556
557end:
57876fd8 558 kfree(param);
d7e09d03 559 }
0a3bdb00 560 return rc;
d7e09d03
PT
561}
562
6e23ea98 563/**
564 * This function will be used to get default LOV/LMV/Default LMV
565 * @valid will be used to indicate which stripe it will retrieve
566 * OBD_MD_MEA LMV stripe EA
567 * OBD_MD_DEFAULT_MEA Default LMV stripe EA
568 * otherwise Default LOV EA.
569 * Each time, it can only retrieve 1 stripe EA
570 **/
571int ll_dir_getstripe(struct inode *inode, void **plmm, int *plmm_size,
572 struct ptlrpc_request **request, u64 valid)
d7e09d03
PT
573{
574 struct ll_sb_info *sbi = ll_i2sbi(inode);
575 struct mdt_body *body;
576 struct lov_mds_md *lmm = NULL;
577 struct ptlrpc_request *req = NULL;
578 int rc, lmmsize;
579 struct md_op_data *op_data;
580
6e23ea98 581 rc = ll_get_max_mdsize(sbi, &lmmsize);
d7e09d03 582 if (rc)
0a3bdb00 583 return rc;
d7e09d03
PT
584
585 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
586 0, lmmsize, LUSTRE_OPC_ANY,
587 NULL);
588 if (IS_ERR(op_data))
0a3bdb00 589 return PTR_ERR(op_data);
d7e09d03
PT
590
591 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
592 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
593 ll_finish_md_op_data(op_data);
594 if (rc < 0) {
97a075cd
JN
595 CDEBUG(D_INFO, "md_getattr failed on inode "DFID": rc %d\n",
596 PFID(ll_inode2fid(inode)), rc);
34e1f2bb 597 goto out;
d7e09d03
PT
598 }
599
600 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
d7e09d03 601
2e1b5b8b 602 lmmsize = body->mbo_eadatasize;
d7e09d03 603
2e1b5b8b 604 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
d7e09d03 605 lmmsize == 0) {
34e1f2bb
JL
606 rc = -ENODATA;
607 goto out;
d7e09d03
PT
608 }
609
610 lmm = req_capsule_server_sized_get(&req->rq_pill,
611 &RMF_MDT_MD, lmmsize);
6e23ea98 612 LASSERT(lmm);
d7e09d03
PT
613
614 /*
615 * This is coming from the MDS, so is probably in
616 * little endian. We convert it to host endian before
617 * passing it to userspace.
618 */
619 /* We don't swab objects for directories */
620 switch (le32_to_cpu(lmm->lmm_magic)) {
621 case LOV_MAGIC_V1:
1f6eaf83 622 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
d7e09d03
PT
623 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
624 break;
625 case LOV_MAGIC_V3:
1f6eaf83 626 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC)
d7e09d03
PT
627 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
628 break;
6e23ea98 629 case LMV_USER_MAGIC:
630 if (cpu_to_le32(LMV_USER_MAGIC) != LMV_USER_MAGIC)
631 lustre_swab_lmv_user_md((struct lmv_user_md *)lmm);
632 break;
d7e09d03
PT
633 default:
634 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
635 rc = -EPROTO;
636 }
637out:
6e23ea98 638 *plmm = lmm;
639 *plmm_size = lmmsize;
d7e09d03
PT
640 *request = req;
641 return rc;
642}
643
79496845 644int ll_get_mdt_idx_by_fid(struct ll_sb_info *sbi, const struct lu_fid *fid)
d7e09d03 645{
d7e09d03 646 struct md_op_data *op_data;
6e23ea98 647 int mdt_index, rc;
d7e09d03 648
6e23ea98 649 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
650 if (!op_data)
651 return -ENOMEM;
d7e09d03
PT
652
653 op_data->op_flags |= MF_GET_MDT_IDX;
6e23ea98 654 op_data->op_fid1 = *fid;
d7e09d03 655 rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
6e23ea98 656 mdt_index = op_data->op_mds;
657 kvfree(op_data);
658 if (rc < 0)
0a3bdb00 659 return rc;
6e23ea98 660
661 return mdt_index;
662}
663
664/*
665 * Get MDT index for the inode.
666 */
667int ll_get_mdt_idx(struct inode *inode)
668{
669 return ll_get_mdt_idx_by_fid(ll_i2sbi(inode), ll_inode2fid(inode));
d7e09d03
PT
670}
671
672/**
673 * Generic handler to do any pre-copy work.
674 *
9c379663 675 * It sends a first hsm_progress (with extent length == 0) to coordinator as a
d7e09d03
PT
676 * first information for it that real work has started.
677 *
678 * Moreover, for a ARCHIVE request, it will sample the file data version and
679 * store it in \a copy.
680 *
681 * \return 0 on success.
682 */
683static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
684{
685 struct ll_sb_info *sbi = ll_s2sbi(sb);
686 struct hsm_progress_kernel hpk;
687 int rc;
d7e09d03
PT
688
689 /* Forge a hsm_progress based on data from copy. */
690 hpk.hpk_fid = copy->hc_hai.hai_fid;
691 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
692 hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
693 hpk.hpk_extent.length = 0;
694 hpk.hpk_flags = 0;
695 hpk.hpk_errval = 0;
696 hpk.hpk_data_version = 0;
697
d7e09d03
PT
698 /* For archive request, we need to read the current file version. */
699 if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
700 struct inode *inode;
701 __u64 data_version = 0;
702
703 /* Get inode for this fid */
704 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
705 if (IS_ERR(inode)) {
706 hpk.hpk_flags |= HP_FLAG_RETRY;
707 /* hpk_errval is >= 0 */
708 hpk.hpk_errval = -PTR_ERR(inode);
34e1f2bb
JL
709 rc = PTR_ERR(inode);
710 goto progress;
d7e09d03
PT
711 }
712
713 /* Read current file data version */
e1798006 714 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
d7e09d03
PT
715 iput(inode);
716 if (rc != 0) {
717 CDEBUG(D_HSM, "Could not read file data version of "
55f5a824 718 DFID" (rc = %d). Archive request (%#llx) could not be done.\n",
d7e09d03
PT
719 PFID(&copy->hc_hai.hai_fid), rc,
720 copy->hc_hai.hai_cookie);
721 hpk.hpk_flags |= HP_FLAG_RETRY;
722 /* hpk_errval must be >= 0 */
723 hpk.hpk_errval = -rc;
34e1f2bb 724 goto progress;
d7e09d03
PT
725 }
726
9c379663 727 /* Store in the hsm_copy for later copytool use.
c0894c6c
OD
728 * Always modified even if no lsm.
729 */
d7e09d03
PT
730 copy->hc_data_version = data_version;
731 }
732
733progress:
c5300d55
AD
734 /* On error, the request should be considered as completed */
735 if (hpk.hpk_errval > 0)
736 hpk.hpk_flags |= HP_FLAG_COMPLETED;
d7e09d03
PT
737 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
738 &hpk, NULL);
739
0a3bdb00 740 return rc;
d7e09d03
PT
741}
742
743/**
744 * Generic handler to do any post-copy work.
745 *
746 * It will send the last hsm_progress update to coordinator to inform it
747 * that copy is finished and whether it was successful or not.
748 *
749 * Moreover,
750 * - for ARCHIVE request, it will sample the file data version and compare it
751 * with the version saved in ll_ioc_copy_start(). If they do not match, copy
752 * will be considered as failed.
753 * - for RESTORE request, it will sample the file data version and send it to
754 * coordinator which is useful if the file was imported as 'released'.
755 *
756 * \return 0 on success.
757 */
758static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
759{
760 struct ll_sb_info *sbi = ll_s2sbi(sb);
761 struct hsm_progress_kernel hpk;
762 int rc;
d7e09d03
PT
763
764 /* If you modify the logic here, also check llapi_hsm_copy_end(). */
765 /* Take care: copy->hc_hai.hai_action, len, gid and data are not
766 * initialized if copy_end was called with copy == NULL.
767 */
768
769 /* Forge a hsm_progress based on data from copy. */
770 hpk.hpk_fid = copy->hc_hai.hai_fid;
771 hpk.hpk_cookie = copy->hc_hai.hai_cookie;
772 hpk.hpk_extent = copy->hc_hai.hai_extent;
773 hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
774 hpk.hpk_errval = copy->hc_errval;
775 hpk.hpk_data_version = 0;
776
777 /* For archive request, we need to check the file data was not changed.
778 *
779 * For restore request, we need to send the file data version, this is
780 * useful when the file was created using hsm_import.
781 */
782 if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
783 (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
784 (copy->hc_errval == 0)) {
785 struct inode *inode;
786 __u64 data_version = 0;
787
788 /* Get lsm for this fid */
789 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
790 if (IS_ERR(inode)) {
791 hpk.hpk_flags |= HP_FLAG_RETRY;
792 /* hpk_errval must be >= 0 */
793 hpk.hpk_errval = -PTR_ERR(inode);
34e1f2bb
JL
794 rc = PTR_ERR(inode);
795 goto progress;
d7e09d03
PT
796 }
797
e1798006 798 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
d7e09d03
PT
799 iput(inode);
800 if (rc) {
2d00bd17 801 CDEBUG(D_HSM, "Could not read file data version. Request could not be confirmed.\n");
d7e09d03
PT
802 if (hpk.hpk_errval == 0)
803 hpk.hpk_errval = -rc;
34e1f2bb 804 goto progress;
d7e09d03
PT
805 }
806
9c379663 807 /* Store in the hsm_copy for later copytool use.
c0894c6c
OD
808 * Always modified even if no lsm.
809 */
d7e09d03
PT
810 hpk.hpk_data_version = data_version;
811
812 /* File could have been stripped during archiving, so we need
c0894c6c
OD
813 * to check anyway.
814 */
d7e09d03
PT
815 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
816 (copy->hc_data_version != data_version)) {
2d00bd17 817 CDEBUG(D_HSM, "File data version mismatched. File content was changed during archiving. "
55f5a824 818 DFID", start:%#llx current:%#llx\n",
d7e09d03
PT
819 PFID(&copy->hc_hai.hai_fid),
820 copy->hc_data_version, data_version);
821 /* File was changed, send error to cdt. Do not ask for
822 * retry because if a file is modified frequently,
823 * the cdt will loop on retried archive requests.
824 * The policy engine will ask for a new archive later
825 * when the file will not be modified for some tunable
c0894c6c
OD
826 * time
827 */
d7e09d03
PT
828 /* we do not notify caller */
829 hpk.hpk_flags &= ~HP_FLAG_RETRY;
830 /* hpk_errval must be >= 0 */
831 hpk.hpk_errval = EBUSY;
832 }
d7e09d03
PT
833 }
834
835progress:
836 rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
837 &hpk, NULL);
838
0a3bdb00 839 return rc;
d7e09d03
PT
840}
841
b0337d6c
JH
842static int copy_and_ioctl(int cmd, struct obd_export *exp,
843 const void __user *data, size_t size)
d7e09d03 844{
b0337d6c 845 void *copy;
d7e09d03
PT
846 int rc;
847
c6c7a170
TH
848 copy = memdup_user(data, size);
849 if (IS_ERR(copy))
850 return PTR_ERR(copy);
b0337d6c
JH
851
852 rc = obd_iocontrol(cmd, exp, size, copy, NULL);
97903a26 853 kfree(copy);
b0337d6c 854
d7e09d03
PT
855 return rc;
856}
857
858static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
859{
860 int cmd = qctl->qc_cmd;
861 int type = qctl->qc_type;
862 int id = qctl->qc_id;
863 int valid = qctl->qc_valid;
864 int rc = 0;
d7e09d03
PT
865
866 switch (cmd) {
867 case LUSTRE_Q_INVALIDATE:
868 case LUSTRE_Q_FINVALIDATE:
869 case Q_QUOTAON:
870 case Q_QUOTAOFF:
871 case Q_SETQUOTA:
872 case Q_SETINFO:
341f1f0a 873 if (!capable(CFS_CAP_SYS_ADMIN))
0a3bdb00 874 return -EPERM;
d7e09d03
PT
875 break;
876 case Q_GETQUOTA:
4b1a25f0 877 if (((type == USRQUOTA &&
8b9e418c 878 !uid_eq(current_euid(), make_kuid(&init_user_ns, id))) ||
4b1a25f0
PT
879 (type == GRPQUOTA &&
880 !in_egroup_p(make_kgid(&init_user_ns, id)))) &&
341f1f0a 881 !capable(CFS_CAP_SYS_ADMIN))
0a3bdb00 882 return -EPERM;
d7e09d03
PT
883 break;
884 case Q_GETINFO:
885 break;
886 default:
887 CERROR("unsupported quotactl op: %#x\n", cmd);
0a3bdb00 888 return -ENOTTY;
d7e09d03
PT
889 }
890
891 if (valid != QC_GENERAL) {
d7e09d03
PT
892 if (cmd == Q_GETINFO)
893 qctl->qc_cmd = Q_GETOINFO;
894 else if (cmd == Q_GETQUOTA)
895 qctl->qc_cmd = Q_GETOQUOTA;
896 else
0a3bdb00 897 return -EINVAL;
d7e09d03
PT
898
899 switch (valid) {
900 case QC_MDTIDX:
901 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
902 sizeof(*qctl), qctl, NULL);
903 break;
904 case QC_OSTIDX:
905 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
906 sizeof(*qctl), qctl, NULL);
907 break;
908 case QC_UUID:
909 rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
910 sizeof(*qctl), qctl, NULL);
911 if (rc == -EAGAIN)
912 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
913 sbi->ll_dt_exp,
914 sizeof(*qctl), qctl, NULL);
915 break;
916 default:
917 rc = -EINVAL;
918 break;
919 }
920
921 if (rc)
0a3bdb00 922 return rc;
d7e09d03
PT
923
924 qctl->qc_cmd = cmd;
925 } else {
926 struct obd_quotactl *oqctl;
927
496a51bd
JL
928 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
929 if (!oqctl)
0a3bdb00 930 return -ENOMEM;
d7e09d03
PT
931
932 QCTL_COPY(oqctl, qctl);
933 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
934 if (rc) {
935 if (rc != -EALREADY && cmd == Q_QUOTAON) {
936 oqctl->qc_cmd = Q_QUOTAOFF;
937 obd_quotactl(sbi->ll_md_exp, oqctl);
938 }
97903a26 939 kfree(oqctl);
0a3bdb00 940 return rc;
d7e09d03
PT
941 }
942 /* If QIF_SPACE is not set, client should collect the
c0894c6c
OD
943 * space usage from OSSs by itself
944 */
d7e09d03
PT
945 if (cmd == Q_GETQUOTA &&
946 !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
947 !oqctl->qc_dqblk.dqb_curspace) {
948 struct obd_quotactl *oqctl_tmp;
949
496a51bd
JL
950 oqctl_tmp = kzalloc(sizeof(*oqctl_tmp), GFP_NOFS);
951 if (!oqctl_tmp) {
34e1f2bb
JL
952 rc = -ENOMEM;
953 goto out;
954 }
d7e09d03
PT
955
956 oqctl_tmp->qc_cmd = Q_GETOQUOTA;
957 oqctl_tmp->qc_id = oqctl->qc_id;
958 oqctl_tmp->qc_type = oqctl->qc_type;
959
960 /* collect space usage from OSTs */
961 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
962 rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
963 if (!rc || rc == -EREMOTEIO) {
964 oqctl->qc_dqblk.dqb_curspace =
965 oqctl_tmp->qc_dqblk.dqb_curspace;
966 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
967 }
968
969 /* collect space & inode usage from MDTs */
970 oqctl_tmp->qc_dqblk.dqb_curspace = 0;
971 oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
972 rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
973 if (!rc || rc == -EREMOTEIO) {
974 oqctl->qc_dqblk.dqb_curspace +=
975 oqctl_tmp->qc_dqblk.dqb_curspace;
976 oqctl->qc_dqblk.dqb_curinodes =
977 oqctl_tmp->qc_dqblk.dqb_curinodes;
978 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
979 } else {
980 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
981 }
982
97903a26 983 kfree(oqctl_tmp);
d7e09d03
PT
984 }
985out:
986 QCTL_COPY(qctl, oqctl);
97903a26 987 kfree(oqctl);
d7e09d03
PT
988 }
989
0a3bdb00 990 return rc;
d7e09d03
PT
991}
992
a7503434
OD
993/* This function tries to get a single name component,
994 * to send to the server. No actual path traversal involved,
c0894c6c
OD
995 * so we limit to NAME_MAX
996 */
a7503434 997static char *ll_getname(const char __user *filename)
d7e09d03
PT
998{
999 int ret = 0, len;
a7503434 1000 char *tmp;
d7e09d03 1001
a7503434 1002 tmp = kzalloc(NAME_MAX + 1, GFP_KERNEL);
d7e09d03
PT
1003 if (!tmp)
1004 return ERR_PTR(-ENOMEM);
1005
a7503434
OD
1006 len = strncpy_from_user(tmp, filename, NAME_MAX + 1);
1007 if (len < 0)
1008 ret = len;
1009 else if (len == 0)
d7e09d03 1010 ret = -ENOENT;
a7503434 1011 else if (len > NAME_MAX && tmp[NAME_MAX] != 0)
d7e09d03
PT
1012 ret = -ENAMETOOLONG;
1013
1014 if (ret) {
a7503434 1015 kfree(tmp);
d7e09d03
PT
1016 tmp = ERR_PTR(ret);
1017 }
1018 return tmp;
1019}
1020
a7503434 1021#define ll_putname(filename) kfree(filename)
d7e09d03
PT
1022
1023static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1024{
2a8a3597 1025 struct inode *inode = file_inode(file);
d7e09d03
PT
1026 struct ll_sb_info *sbi = ll_i2sbi(inode);
1027 struct obd_ioctl_data *data;
1028 int rc = 0;
d7e09d03 1029
97a075cd
JN
1030 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
1031 PFID(ll_inode2fid(inode)), inode, cmd);
d7e09d03
PT
1032
1033 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
1034 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
1035 return -ENOTTY;
1036
1037 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
a58a38ac 1038 switch (cmd) {
d7e09d03
PT
1039 case FSFILT_IOC_GETFLAGS:
1040 case FSFILT_IOC_SETFLAGS:
0a3bdb00 1041 return ll_iocontrol(inode, file, cmd, arg);
d7e09d03
PT
1042 case FSFILT_IOC_GETVERSION_OLD:
1043 case FSFILT_IOC_GETVERSION:
af00f6c5 1044 return put_user(inode->i_generation, (int __user *)arg);
d7e09d03
PT
1045 /* We need to special case any other ioctls we want to handle,
1046 * to send them to the MDS/OST as appropriate and to properly
1047 * network encode the arg field.
1048 case FSFILT_IOC_SETVERSION_OLD:
1049 case FSFILT_IOC_SETVERSION:
1050 */
1051 case LL_IOC_GET_MDTIDX: {
1052 int mdtidx;
1053
1054 mdtidx = ll_get_mdt_idx(inode);
1055 if (mdtidx < 0)
0a3bdb00 1056 return mdtidx;
d7e09d03 1057
af00f6c5 1058 if (put_user((int)mdtidx, (int __user *)arg))
0a3bdb00 1059 return -EFAULT;
d7e09d03
PT
1060
1061 return 0;
1062 }
1063 case IOC_MDC_LOOKUP: {
d7e09d03
PT
1064 int namelen, len = 0;
1065 char *buf = NULL;
1066 char *filename;
d7e09d03 1067
e3e8ff41 1068 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
d7e09d03 1069 if (rc)
0a3bdb00 1070 return rc;
d7e09d03
PT
1071 data = (void *)buf;
1072
1073 filename = data->ioc_inlbuf1;
1074 namelen = strlen(filename);
1075
1076 if (namelen < 1) {
1077 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
34e1f2bb
JL
1078 rc = -EINVAL;
1079 goto out_free;
d7e09d03
PT
1080 }
1081
1d82425f 1082 rc = ll_get_fid_by_name(inode, filename, namelen, NULL);
d7e09d03 1083 if (rc < 0) {
1d82425f 1084 CERROR("%s: lookup %.*s failed: rc = %d\n",
1085 ll_get_fsname(inode->i_sb, NULL, 0), namelen,
1086 filename, rc);
34e1f2bb 1087 goto out_free;
d7e09d03 1088 }
d7e09d03
PT
1089out_free:
1090 obd_ioctl_freedata(buf, len);
1091 return rc;
1092 }
1093 case LL_IOC_LMV_SETSTRIPE: {
1094 struct lmv_user_md *lum;
1095 char *buf = NULL;
1096 char *filename;
1097 int namelen = 0;
1098 int lumlen = 0;
d07d4cb8 1099 umode_t mode;
d7e09d03
PT
1100 int len;
1101 int rc;
1102
e3e8ff41 1103 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
d7e09d03 1104 if (rc)
0a3bdb00 1105 return rc;
d7e09d03
PT
1106
1107 data = (void *)buf;
6e16818b 1108 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
34e1f2bb
JL
1109 data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0) {
1110 rc = -EINVAL;
1111 goto lmv_out_free;
1112 }
d7e09d03
PT
1113
1114 filename = data->ioc_inlbuf1;
1115 namelen = data->ioc_inllen1;
1116
1117 if (namelen < 1) {
1118 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
34e1f2bb
JL
1119 rc = -EINVAL;
1120 goto lmv_out_free;
d7e09d03
PT
1121 }
1122 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1123 lumlen = data->ioc_inllen2;
1124
1125 if (lum->lum_magic != LMV_USER_MAGIC ||
1126 lumlen != sizeof(*lum)) {
1127 CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1128 filename, lum->lum_magic, lumlen, -EFAULT);
34e1f2bb
JL
1129 rc = -EINVAL;
1130 goto lmv_out_free;
d7e09d03
PT
1131 }
1132
d07d4cb8 1133#if OBD_OCD_VERSION(2, 9, 50, 0) > LUSTRE_VERSION_CODE
1134 mode = data->ioc_type != 0 ? data->ioc_type : S_IRWXUGO;
1135#else
1136 mode = data->ioc_type;
1137#endif
1138 rc = ll_dir_setdirstripe(inode, lum, filename, mode);
d7e09d03
PT
1139lmv_out_free:
1140 obd_ioctl_freedata(buf, len);
0a3bdb00 1141 return rc;
d7e09d03 1142 }
6e23ea98 1143 case LL_IOC_LMV_SET_DEFAULT_STRIPE: {
1144 struct lmv_user_md __user *ulump;
1145 struct lmv_user_md lum;
1146 int rc;
1147
1148 ulump = (struct lmv_user_md __user *)arg;
1149 if (copy_from_user(&lum, ulump, sizeof(lum)))
1150 return -EFAULT;
1151
1152 if (lum.lum_magic != LMV_USER_MAGIC)
1153 return -EINVAL;
1154
1155 rc = ll_dir_setstripe(inode, (struct lov_user_md *)&lum, 0);
1156
1157 return rc;
1158 }
d7e09d03
PT
1159 case LL_IOC_LOV_SETSTRIPE: {
1160 struct lov_user_md_v3 lumv3;
1161 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
af00f6c5
OD
1162 struct lov_user_md_v1 __user *lumv1p = (void __user *)arg;
1163 struct lov_user_md_v3 __user *lumv3p = (void __user *)arg;
d7e09d03
PT
1164
1165 int set_default = 0;
1166
1167 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1168 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1169 sizeof(lumv3p->lmm_objects[0]));
1170 /* first try with v1 which is smaller than v3 */
1171 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
0a3bdb00 1172 return -EFAULT;
d7e09d03 1173
557732ad 1174 if (lumv1->lmm_magic == LOV_USER_MAGIC_V3) {
d7e09d03 1175 if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
0a3bdb00 1176 return -EFAULT;
d7e09d03
PT
1177 }
1178
f76c23da 1179 if (is_root_inode(inode))
d7e09d03
PT
1180 set_default = 1;
1181
1182 /* in v1 and v3 cases lumv1 points to data */
1183 rc = ll_dir_setstripe(inode, lumv1, set_default);
1184
0a3bdb00 1185 return rc;
d7e09d03
PT
1186 }
1187 case LL_IOC_LMV_GETSTRIPE: {
6e23ea98 1188 struct lmv_user_md __user *ulmv;
d7e09d03 1189 struct lmv_user_md lum;
6e23ea98 1190 struct ptlrpc_request *request = NULL;
1191 struct lmv_user_md *tmp = NULL;
1192 union lmv_mds_md *lmm = NULL;
1193 u64 valid = 0;
1194 int stripe_count;
1195 int mdt_index;
d7e09d03 1196 int lum_size;
6e23ea98 1197 int lmmsize;
1198 int rc;
1199 int i;
d7e09d03 1200
6e23ea98 1201 ulmv = (struct lmv_user_md __user *)arg;
1202 if (copy_from_user(&lum, ulmv, sizeof(*ulmv)))
0a3bdb00 1203 return -EFAULT;
d7e09d03 1204
6e23ea98 1205 /*
1206 * lum_magic will indicate which stripe the ioctl will like
1207 * to get, LMV_MAGIC_V1 is for normal LMV stripe, LMV_USER_MAGIC
1208 * is for default LMV stripe
1209 */
1210 if (lum.lum_magic == LMV_MAGIC_V1)
1211 valid |= OBD_MD_MEA;
1212 else if (lum.lum_magic == LMV_USER_MAGIC)
1213 valid |= OBD_MD_DEFAULT_MEA;
1214 else
0a3bdb00 1215 return -EINVAL;
d7e09d03 1216
6e23ea98 1217 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
1218 valid);
8f18c8a4 1219 if (rc)
6e23ea98 1220 goto finish_req;
1221
1222 /* Get default LMV EA */
1223 if (lum.lum_magic == LMV_USER_MAGIC) {
1224 if (rc)
1225 goto finish_req;
1226
1227 if (lmmsize > sizeof(*ulmv)) {
1228 rc = -EINVAL;
1229 goto finish_req;
1230 }
1231
1232 if (copy_to_user(ulmv, lmm, lmmsize))
1233 rc = -EFAULT;
1234
1235 goto finish_req;
1236 }
1237
8f18c8a4 1238 stripe_count = lmv_mds_md_stripe_count_get(lmm);
6e23ea98 1239 lum_size = lmv_user_md_size(stripe_count, LMV_MAGIC_V1);
496a51bd
JL
1240 tmp = kzalloc(lum_size, GFP_NOFS);
1241 if (!tmp) {
34e1f2bb 1242 rc = -ENOMEM;
6e23ea98 1243 goto finish_req;
34e1f2bb 1244 }
d7e09d03 1245
6e23ea98 1246 mdt_index = ll_get_mdt_idx(inode);
1247 if (mdt_index < 0) {
34e1f2bb 1248 rc = -ENOMEM;
6e23ea98 1249 goto out_tmp;
1250 }
8f18c8a4 1251 tmp->lum_magic = LMV_MAGIC_V1;
1252 tmp->lum_stripe_count = 0;
6e23ea98 1253 tmp->lum_stripe_offset = mdt_index;
8f18c8a4 1254 for (i = 0; i < stripe_count; i++) {
1255 struct lu_fid *fid;
1256
1257 fid = &lmm->lmv_md_v1.lmv_stripe_fids[i];
1258 mdt_index = ll_get_mdt_idx_by_fid(sbi, fid);
6e23ea98 1259 if (mdt_index < 0) {
1260 rc = mdt_index;
1261 goto out_tmp;
1262 }
1263 tmp->lum_objects[i].lum_mds = mdt_index;
8f18c8a4 1264 tmp->lum_objects[i].lum_fid = *fid;
6e23ea98 1265 tmp->lum_stripe_count++;
34e1f2bb 1266 }
d7e09d03 1267
6e23ea98 1268 if (copy_to_user(ulmv, tmp, lum_size)) {
34e1f2bb 1269 rc = -EFAULT;
6e23ea98 1270 goto out_tmp;
34e1f2bb 1271 }
6e23ea98 1272out_tmp:
fd5e2fd0 1273 kfree(tmp);
6e23ea98 1274finish_req:
1275 ptlrpc_req_finished(request);
0a3bdb00 1276 return rc;
d7e09d03 1277 }
6e23ea98 1278
d7e09d03 1279 case LL_IOC_LOV_SWAP_LAYOUTS:
0a3bdb00 1280 return -EPERM;
8877d3bf 1281 case IOC_OBD_STATFS:
4c6243ec 1282 return ll_obd_statfs(inode, (void __user *)arg);
d7e09d03
PT
1283 case LL_IOC_LOV_GETSTRIPE:
1284 case LL_IOC_MDC_GETINFO:
1285 case IOC_MDC_GETFILEINFO:
1286 case IOC_MDC_GETFILESTRIPE: {
1287 struct ptlrpc_request *request = NULL;
af00f6c5 1288 struct lov_user_md __user *lump;
d7e09d03
PT
1289 struct lov_mds_md *lmm = NULL;
1290 struct mdt_body *body;
1291 char *filename = NULL;
1292 int lmmsize;
1293
1294 if (cmd == IOC_MDC_GETFILEINFO ||
1295 cmd == IOC_MDC_GETFILESTRIPE) {
d47bb83b 1296 filename = ll_getname((const char __user *)arg);
d7e09d03 1297 if (IS_ERR(filename))
0a3bdb00 1298 return PTR_ERR(filename);
d7e09d03
PT
1299
1300 rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1301 &lmmsize, &request);
1302 } else {
6e23ea98 1303 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
1304 &request, 0);
d7e09d03
PT
1305 }
1306
1307 if (request) {
1308 body = req_capsule_server_get(&request->rq_pill,
1309 &RMF_MDT_BODY);
6e16818b 1310 LASSERT(body);
d7e09d03 1311 } else {
34e1f2bb 1312 goto out_req;
d7e09d03
PT
1313 }
1314
1315 if (rc < 0) {
1316 if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
34e1f2bb
JL
1317 cmd == LL_IOC_MDC_GETINFO)) {
1318 rc = 0;
1319 goto skip_lmm;
da5ecb4d 1320 } else {
34e1f2bb 1321 goto out_req;
da5ecb4d 1322 }
d7e09d03
PT
1323 }
1324
1325 if (cmd == IOC_MDC_GETFILESTRIPE ||
1326 cmd == LL_IOC_LOV_GETSTRIPE) {
af00f6c5 1327 lump = (struct lov_user_md __user *)arg;
d7e09d03 1328 } else {
af00f6c5 1329 struct lov_user_mds_data __user *lmdp;
79792190 1330
af00f6c5 1331 lmdp = (struct lov_user_mds_data __user *)arg;
d7e09d03
PT
1332 lump = &lmdp->lmd_lmm;
1333 }
1334 if (copy_to_user(lump, lmm, lmmsize)) {
34e1f2bb
JL
1335 if (copy_to_user(lump, lmm, sizeof(*lump))) {
1336 rc = -EFAULT;
1337 goto out_req;
1338 }
d7e09d03
PT
1339 rc = -EOVERFLOW;
1340 }
eb73f514 1341skip_lmm:
d7e09d03 1342 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
af00f6c5 1343 struct lov_user_mds_data __user *lmdp;
d7e09d03
PT
1344 lstat_t st = { 0 };
1345
1346 st.st_dev = inode->i_sb->s_dev;
2e1b5b8b
JH
1347 st.st_mode = body->mbo_mode;
1348 st.st_nlink = body->mbo_nlink;
1349 st.st_uid = body->mbo_uid;
1350 st.st_gid = body->mbo_gid;
1351 st.st_rdev = body->mbo_rdev;
1352 st.st_size = body->mbo_size;
09cbfeaf 1353 st.st_blksize = PAGE_SIZE;
2e1b5b8b
JH
1354 st.st_blocks = body->mbo_blocks;
1355 st.st_atime = body->mbo_atime;
1356 st.st_mtime = body->mbo_mtime;
1357 st.st_ctime = body->mbo_ctime;
1358 st.st_ino = cl_fid_build_ino(&body->mbo_fid1,
d780846e 1359 sbi->ll_flags &
1360 LL_SBI_32BIT_API);
d7e09d03 1361
af00f6c5 1362 lmdp = (struct lov_user_mds_data __user *)arg;
34e1f2bb
JL
1363 if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st))) {
1364 rc = -EFAULT;
1365 goto out_req;
1366 }
d7e09d03
PT
1367 }
1368
eb73f514 1369out_req:
d7e09d03
PT
1370 ptlrpc_req_finished(request);
1371 if (filename)
1372 ll_putname(filename);
1373 return rc;
1374 }
1375 case IOC_LOV_GETINFO: {
af00f6c5 1376 struct lov_user_mds_data __user *lumd;
d7e09d03 1377 struct lov_stripe_md *lsm;
af00f6c5 1378 struct lov_user_md __user *lum;
d7e09d03
PT
1379 struct lov_mds_md *lmm;
1380 int lmmsize;
1381 lstat_t st;
1382
af00f6c5 1383 lumd = (struct lov_user_mds_data __user *)arg;
d7e09d03
PT
1384 lum = &lumd->lmd_lmm;
1385
1386 rc = ll_get_max_mdsize(sbi, &lmmsize);
1387 if (rc)
0a3bdb00 1388 return rc;
d7e09d03 1389
e958f49b 1390 lmm = libcfs_kvzalloc(lmmsize, GFP_NOFS);
6e16818b 1391 if (!lmm)
0a3bdb00 1392 return -ENOMEM;
34e1f2bb
JL
1393 if (copy_from_user(lmm, lum, lmmsize)) {
1394 rc = -EFAULT;
1395 goto free_lmm;
1396 }
d7e09d03
PT
1397
1398 switch (lmm->lmm_magic) {
1399 case LOV_USER_MAGIC_V1:
1f6eaf83 1400 if (cpu_to_le32(LOV_USER_MAGIC_V1) == LOV_USER_MAGIC_V1)
d7e09d03
PT
1401 break;
1402 /* swab objects first so that stripes num will be sane */
1403 lustre_swab_lov_user_md_objects(
1404 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1405 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1406 lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1407 break;
1408 case LOV_USER_MAGIC_V3:
1f6eaf83 1409 if (cpu_to_le32(LOV_USER_MAGIC_V3) == LOV_USER_MAGIC_V3)
d7e09d03
PT
1410 break;
1411 /* swab objects first so that stripes num will be sane */
1412 lustre_swab_lov_user_md_objects(
1413 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1414 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1415 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1416 break;
1417 default:
34e1f2bb
JL
1418 rc = -EINVAL;
1419 goto free_lmm;
d7e09d03
PT
1420 }
1421
1422 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
34e1f2bb
JL
1423 if (rc < 0) {
1424 rc = -ENOMEM;
1425 goto free_lmm;
1426 }
d7e09d03
PT
1427
1428 /* Perform glimpse_size operation. */
1429 memset(&st, 0, sizeof(st));
1430
1431 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1432 if (rc)
34e1f2bb 1433 goto free_lsm;
d7e09d03 1434
34e1f2bb
JL
1435 if (copy_to_user(&lumd->lmd_st, &st, sizeof(st))) {
1436 rc = -EFAULT;
1437 goto free_lsm;
1438 }
d7e09d03 1439
eb73f514 1440free_lsm:
d7e09d03 1441 obd_free_memmd(sbi->ll_dt_exp, &lsm);
eb73f514 1442free_lmm:
e958f49b 1443 kvfree(lmm);
d7e09d03
PT
1444 return rc;
1445 }
d7e09d03
PT
1446 case OBD_IOC_QUOTACHECK: {
1447 struct obd_quotactl *oqctl;
1448 int error = 0;
1449
341f1f0a 1450 if (!capable(CFS_CAP_SYS_ADMIN))
0a3bdb00 1451 return -EPERM;
d7e09d03 1452
496a51bd 1453 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
d7e09d03 1454 if (!oqctl)
0a3bdb00 1455 return -ENOMEM;
d7e09d03
PT
1456 oqctl->qc_type = arg;
1457 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1458 if (rc < 0) {
1459 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1460 error = rc;
1461 }
1462
1463 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1464 if (rc < 0)
1465 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1466
97903a26 1467 kfree(oqctl);
d7e09d03
PT
1468 return error ?: rc;
1469 }
1470 case OBD_IOC_POLL_QUOTACHECK: {
1471 struct if_quotacheck *check;
1472
341f1f0a 1473 if (!capable(CFS_CAP_SYS_ADMIN))
0a3bdb00 1474 return -EPERM;
d7e09d03 1475
496a51bd 1476 check = kzalloc(sizeof(*check), GFP_NOFS);
d7e09d03 1477 if (!check)
0a3bdb00 1478 return -ENOMEM;
d7e09d03
PT
1479
1480 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1481 NULL);
1482 if (rc) {
1483 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
af00f6c5
OD
1484 if (copy_to_user((void __user *)arg, check,
1485 sizeof(*check)))
d7e09d03 1486 CDEBUG(D_QUOTA, "copy_to_user failed\n");
34e1f2bb 1487 goto out_poll;
d7e09d03
PT
1488 }
1489
1490 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1491 NULL);
1492 if (rc) {
1493 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
af00f6c5
OD
1494 if (copy_to_user((void __user *)arg, check,
1495 sizeof(*check)))
d7e09d03 1496 CDEBUG(D_QUOTA, "copy_to_user failed\n");
34e1f2bb 1497 goto out_poll;
d7e09d03 1498 }
eb73f514 1499out_poll:
97903a26 1500 kfree(check);
0a3bdb00 1501 return rc;
d7e09d03 1502 }
8877d3bf 1503 case OBD_IOC_QUOTACTL: {
d7e09d03
PT
1504 struct if_quotactl *qctl;
1505
496a51bd 1506 qctl = kzalloc(sizeof(*qctl), GFP_NOFS);
d7e09d03 1507 if (!qctl)
0a3bdb00 1508 return -ENOMEM;
d7e09d03 1509
af00f6c5 1510 if (copy_from_user(qctl, (void __user *)arg, sizeof(*qctl))) {
34e1f2bb
JL
1511 rc = -EFAULT;
1512 goto out_quotactl;
1513 }
d7e09d03
PT
1514
1515 rc = quotactl_ioctl(sbi, qctl);
1516
af00f6c5
OD
1517 if (rc == 0 && copy_to_user((void __user *)arg, qctl,
1518 sizeof(*qctl)))
d7e09d03
PT
1519 rc = -EFAULT;
1520
eb73f514 1521out_quotactl:
97903a26 1522 kfree(qctl);
0a3bdb00 1523 return rc;
d7e09d03
PT
1524 }
1525 case OBD_IOC_GETDTNAME:
1526 case OBD_IOC_GETMDNAME:
0a3bdb00 1527 return ll_get_obd_name(inode, cmd, arg);
d7e09d03 1528 case LL_IOC_FLUSHCTX:
0a3bdb00 1529 return ll_flush_ctx(inode);
d7e09d03
PT
1530 case LL_IOC_GETOBDCOUNT: {
1531 int count, vallen;
1532 struct obd_export *exp;
1533
af00f6c5 1534 if (copy_from_user(&count, (int __user *)arg, sizeof(int)))
0a3bdb00 1535 return -EFAULT;
d7e09d03
PT
1536
1537 /* get ost count when count is zero, get mdt count otherwise */
1538 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1539 vallen = sizeof(count);
1540 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1541 KEY_TGT_COUNT, &vallen, &count, NULL);
1542 if (rc) {
1543 CERROR("get target count failed: %d\n", rc);
0a3bdb00 1544 return rc;
d7e09d03
PT
1545 }
1546
af00f6c5 1547 if (copy_to_user((int __user *)arg, &count, sizeof(int)))
0a3bdb00 1548 return -EFAULT;
d7e09d03 1549
0a3bdb00 1550 return 0;
d7e09d03
PT
1551 }
1552 case LL_IOC_PATH2FID:
af00f6c5
OD
1553 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
1554 sizeof(struct lu_fid)))
0a3bdb00
GKH
1555 return -EFAULT;
1556 return 0;
d7e09d03 1557 case LL_IOC_GET_CONNECT_FLAGS: {
e09bee34
OD
1558 return obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL,
1559 (void __user *)arg);
d7e09d03
PT
1560 }
1561 case OBD_IOC_CHANGELOG_SEND:
1562 case OBD_IOC_CHANGELOG_CLEAR:
bbaa9c10
NY
1563 if (!capable(CFS_CAP_SYS_ADMIN))
1564 return -EPERM;
1565
af00f6c5 1566 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
d7e09d03 1567 sizeof(struct ioc_changelog));
0a3bdb00 1568 return rc;
d7e09d03 1569 case OBD_IOC_FID2PATH:
7ec89fa5 1570 return ll_fid2path(inode, (void __user *)arg);
d7e09d03
PT
1571 case LL_IOC_HSM_REQUEST: {
1572 struct hsm_user_request *hur;
6b2eb32e 1573 ssize_t totalsize;
d7e09d03 1574
af00f6c5 1575 hur = memdup_user((void __user *)arg, sizeof(*hur));
4a07594e
AH
1576 if (IS_ERR(hur))
1577 return PTR_ERR(hur);
d7e09d03
PT
1578
1579 /* Compute the whole struct size */
1580 totalsize = hur_len(hur);
97903a26 1581 kfree(hur);
6b2eb32e
NC
1582 if (totalsize < 0)
1583 return -E2BIG;
e55c4476
JN
1584
1585 /* Final size will be more than double totalsize */
1586 if (totalsize >= MDS_MAXREQSIZE / 3)
1587 return -E2BIG;
1588
e958f49b 1589 hur = libcfs_kvzalloc(totalsize, GFP_NOFS);
6e16818b 1590 if (!hur)
0a3bdb00 1591 return -ENOMEM;
d7e09d03
PT
1592
1593 /* Copy the whole struct */
af00f6c5 1594 if (copy_from_user(hur, (void __user *)arg, totalsize)) {
e958f49b 1595 kvfree(hur);
0a3bdb00 1596 return -EFAULT;
d7e09d03
PT
1597 }
1598
48d23e61
JX
1599 if (hur->hur_request.hr_action == HUA_RELEASE) {
1600 const struct lu_fid *fid;
1601 struct inode *f;
1602 int i;
1603
1604 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1605 fid = &hur->hur_user_item[i].hui_fid;
1606 f = search_inode_for_lustre(inode->i_sb, fid);
1607 if (IS_ERR(f)) {
1608 rc = PTR_ERR(f);
1609 break;
1610 }
1611
1612 rc = ll_hsm_release(f);
1613 iput(f);
1614 if (rc != 0)
1615 break;
1616 }
1617 } else {
1618 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1619 hur, NULL);
1620 }
d7e09d03 1621
e958f49b 1622 kvfree(hur);
d7e09d03 1623
0a3bdb00 1624 return rc;
d7e09d03
PT
1625 }
1626 case LL_IOC_HSM_PROGRESS: {
1627 struct hsm_progress_kernel hpk;
1628 struct hsm_progress hp;
1629
af00f6c5 1630 if (copy_from_user(&hp, (void __user *)arg, sizeof(hp)))
0a3bdb00 1631 return -EFAULT;
d7e09d03
PT
1632
1633 hpk.hpk_fid = hp.hp_fid;
1634 hpk.hpk_cookie = hp.hp_cookie;
1635 hpk.hpk_extent = hp.hp_extent;
1636 hpk.hpk_flags = hp.hp_flags;
1637 hpk.hpk_errval = hp.hp_errval;
1638 hpk.hpk_data_version = 0;
1639
1640 /* File may not exist in Lustre; all progress
c0894c6c
OD
1641 * reported to Lustre root
1642 */
d7e09d03
PT
1643 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1644 NULL);
0a3bdb00 1645 return rc;
d7e09d03
PT
1646 }
1647 case LL_IOC_HSM_CT_START:
1cc28b77
JH
1648 if (!capable(CFS_CAP_SYS_ADMIN))
1649 return -EPERM;
1650
af00f6c5 1651 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void __user *)arg,
d7e09d03 1652 sizeof(struct lustre_kernelcomm));
0a3bdb00 1653 return rc;
d7e09d03
PT
1654
1655 case LL_IOC_HSM_COPY_START: {
1656 struct hsm_copy *copy;
1657 int rc;
1658
af00f6c5 1659 copy = memdup_user((char __user *)arg, sizeof(*copy));
4a07594e
AH
1660 if (IS_ERR(copy))
1661 return PTR_ERR(copy);
d7e09d03
PT
1662
1663 rc = ll_ioc_copy_start(inode->i_sb, copy);
af00f6c5 1664 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
d7e09d03
PT
1665 rc = -EFAULT;
1666
97903a26 1667 kfree(copy);
0a3bdb00 1668 return rc;
d7e09d03
PT
1669 }
1670 case LL_IOC_HSM_COPY_END: {
1671 struct hsm_copy *copy;
1672 int rc;
1673
af00f6c5 1674 copy = memdup_user((char __user *)arg, sizeof(*copy));
4a07594e
AH
1675 if (IS_ERR(copy))
1676 return PTR_ERR(copy);
d7e09d03
PT
1677
1678 rc = ll_ioc_copy_end(inode->i_sb, copy);
af00f6c5 1679 if (copy_to_user((char __user *)arg, copy, sizeof(*copy)))
d7e09d03
PT
1680 rc = -EFAULT;
1681
97903a26 1682 kfree(copy);
0a3bdb00 1683 return rc;
d7e09d03 1684 }
79496845 1685 case LL_IOC_MIGRATE: {
1686 char *buf = NULL;
1687 const char *filename;
1688 int namelen = 0;
1689 int len;
1690 int rc;
1691 int mdtidx;
1692
1693 rc = obd_ioctl_getdata(&buf, &len, (void __user *)arg);
1694 if (rc < 0)
1695 return rc;
1696
1697 data = (struct obd_ioctl_data *)buf;
1698 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1699 !data->ioc_inllen1 || !data->ioc_inllen2) {
1700 rc = -EINVAL;
1701 goto migrate_free;
1702 }
1703
1704 filename = data->ioc_inlbuf1;
1705 namelen = data->ioc_inllen1;
1d82425f 1706 if (namelen < 1 || namelen != strlen(filename) + 1) {
79496845 1707 rc = -EINVAL;
1708 goto migrate_free;
1709 }
1710
1711 if (data->ioc_inllen2 != sizeof(mdtidx)) {
1712 rc = -EINVAL;
1713 goto migrate_free;
1714 }
1715 mdtidx = *(int *)data->ioc_inlbuf2;
1716
1d82425f 1717 rc = ll_migrate(inode, file, mdtidx, filename, namelen - 1);
79496845 1718migrate_free:
1719 obd_ioctl_freedata(buf, len);
1720
1721 return rc;
1722 }
1723
d7e09d03 1724 default:
e09bee34
OD
1725 return obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1726 (void __user *)arg);
d7e09d03
PT
1727 }
1728}
1729
1730static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1731{
1732 struct inode *inode = file->f_mapping->host;
1733 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1734 struct ll_sb_info *sbi = ll_i2sbi(inode);
1735 int api32 = ll_need_32bit_api(sbi);
1736 loff_t ret = -EINVAL;
d7e09d03 1737
d7e09d03 1738 switch (origin) {
e17f5594 1739 case SEEK_SET:
1740 break;
1741 case SEEK_CUR:
1742 offset += file->f_pos;
1743 break;
1744 case SEEK_END:
1745 if (offset > 0)
34e1f2bb 1746 goto out;
e17f5594 1747 if (api32)
1748 offset += LL_DIR_END_OFF_32BIT;
1749 else
1750 offset += LL_DIR_END_OFF;
1751 break;
1752 default:
1753 goto out;
d7e09d03
PT
1754 }
1755
1756 if (offset >= 0 &&
1757 ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1758 (!api32 && offset <= LL_DIR_END_OFF))) {
1759 if (offset != file->f_pos) {
1760 if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1761 (!api32 && offset == LL_DIR_END_OFF))
1762 fd->lfd_pos = MDS_DIR_END_OFF;
1763 else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1764 fd->lfd_pos = offset << 32;
1765 else
1766 fd->lfd_pos = offset;
1767 file->f_pos = offset;
1768 file->f_version = 0;
1769 }
1770 ret = offset;
1771 }
34e1f2bb 1772 goto out;
d7e09d03
PT
1773
1774out:
d7e09d03
PT
1775 return ret;
1776}
1777
2d95f10e 1778static int ll_dir_open(struct inode *inode, struct file *file)
d7e09d03 1779{
0a3bdb00 1780 return ll_file_open(inode, file);
d7e09d03
PT
1781}
1782
2d95f10e 1783static int ll_dir_release(struct inode *inode, struct file *file)
d7e09d03 1784{
0a3bdb00 1785 return ll_file_release(inode, file);
d7e09d03
PT
1786}
1787
2d95f10e 1788const struct file_operations ll_dir_operations = {
d7e09d03
PT
1789 .llseek = ll_dir_seek,
1790 .open = ll_dir_open,
1791 .release = ll_dir_release,
1792 .read = generic_read_dir,
060ff688 1793 .iterate_shared = ll_readdir,
d7e09d03
PT
1794 .unlocked_ioctl = ll_dir_ioctl,
1795 .fsync = ll_fsync,
1796};
This page took 0.622157 seconds and 5 git commands to generate.