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