blob: 4216739e163ce58f5e201ee149927dc0c3a7ec73 [file] [log] [blame]
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001/*
2 * linux/fs/ocfs2/ioctl.c
3 *
4 * Copyright (C) 2006 Herbert Poetzl
5 * adapted from Remy Card's ext2/ioctl.c
6 */
7
8#include <linux/fs.h>
9#include <linux/mount.h>
Tao Ma34e6c592010-01-27 10:21:52 +080010#include <linux/compat.h>
Herbert Poetzlca4d1472006-07-03 17:27:12 -070011
Herbert Poetzlca4d1472006-07-03 17:27:12 -070012#include <cluster/masklog.h>
13
14#include "ocfs2.h"
15#include "alloc.h"
16#include "dlmglue.h"
Mark Fashehb2580102007-03-09 16:53:21 -080017#include "file.h"
Herbert Poetzlca4d1472006-07-03 17:27:12 -070018#include "inode.h"
19#include "journal.h"
20
21#include "ocfs2_fs.h"
Adrian Bunk2d562512006-07-10 01:32:51 +020022#include "ioctl.h"
Tao Mad6590722007-12-18 15:47:03 +080023#include "resize.h"
Tao Mabd508732009-09-21 11:25:14 +080024#include "refcounttree.h"
Tristan Ye3e5db172011-05-24 15:25:54 +080025#include "sysfile.h"
26#include "dir.h"
27#include "buffer_head_io.h"
Adrian Bunk2d562512006-07-10 01:32:51 +020028
Herbert Poetzlca4d1472006-07-03 17:27:12 -070029#include <linux/ext2_fs.h>
30
Tristan Yeddee5cd2010-05-22 16:26:33 +080031#define o2info_from_user(a, b) \
32 copy_from_user(&(a), (b), sizeof(a))
33#define o2info_to_user(a, b) \
34 copy_to_user((typeof(a) __user *)b, &(a), sizeof(a))
35
36/*
37 * This call is void because we are already reporting an error that may
38 * be -EFAULT. The error will be returned from the ioctl(2) call. It's
39 * just a best-effort to tell userspace that this request caused the error.
40 */
Tristan Ye8aa1fa32011-05-24 15:22:59 +080041static inline void o2info_set_request_error(struct ocfs2_info_request *kreq,
Tristan Yeddee5cd2010-05-22 16:26:33 +080042 struct ocfs2_info_request __user *req)
43{
44 kreq->ir_flags |= OCFS2_INFO_FL_ERROR;
45 (void)put_user(kreq->ir_flags, (__u32 __user *)&(req->ir_flags));
46}
47
Tristan Ye8aa1fa32011-05-24 15:22:59 +080048static inline void o2info_set_request_filled(struct ocfs2_info_request *req)
Tristan Ye1936a262011-01-30 14:25:59 +080049{
50 req->ir_flags |= OCFS2_INFO_FL_FILLED;
51}
52
Tristan Ye8aa1fa32011-05-24 15:22:59 +080053static inline void o2info_clear_request_filled(struct ocfs2_info_request *req)
Tristan Ye1936a262011-01-30 14:25:59 +080054{
55 req->ir_flags &= ~OCFS2_INFO_FL_FILLED;
56}
57
Tristan Ye3e5db172011-05-24 15:25:54 +080058static inline int o2info_coherent(struct ocfs2_info_request *req)
59{
60 return (!(req->ir_flags & OCFS2_INFO_FL_NON_COHERENT));
61}
62
Herbert Poetzlca4d1472006-07-03 17:27:12 -070063static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
64{
65 int status;
66
Mark Fashehe63aecb62007-10-18 15:30:42 -070067 status = ocfs2_inode_lock(inode, NULL, 0);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070068 if (status < 0) {
69 mlog_errno(status);
70 return status;
71 }
Jan Kara6e4b0d52007-04-27 11:08:01 -070072 ocfs2_get_inode_flags(OCFS2_I(inode));
Herbert Poetzlca4d1472006-07-03 17:27:12 -070073 *flags = OCFS2_I(inode)->ip_attr;
Mark Fashehe63aecb62007-10-18 15:30:42 -070074 ocfs2_inode_unlock(inode, 0);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070075
Herbert Poetzlca4d1472006-07-03 17:27:12 -070076 return status;
77}
78
79static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
80 unsigned mask)
81{
82 struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
83 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -070084 handle_t *handle = NULL;
Herbert Poetzlca4d1472006-07-03 17:27:12 -070085 struct buffer_head *bh = NULL;
86 unsigned oldflags;
87 int status;
88
89 mutex_lock(&inode->i_mutex);
90
Mark Fashehe63aecb62007-10-18 15:30:42 -070091 status = ocfs2_inode_lock(inode, &bh, 1);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070092 if (status < 0) {
93 mlog_errno(status);
94 goto bail;
95 }
96
Herbert Poetzlca4d1472006-07-03 17:27:12 -070097 status = -EACCES;
Serge E. Hallyn2e149672011-03-23 16:43:26 -070098 if (!inode_owner_or_capable(inode))
Herbert Poetzlca4d1472006-07-03 17:27:12 -070099 goto bail_unlock;
100
101 if (!S_ISDIR(inode->i_mode))
102 flags &= ~OCFS2_DIRSYNC_FL;
103
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700104 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700105 if (IS_ERR(handle)) {
106 status = PTR_ERR(handle);
107 mlog_errno(status);
108 goto bail_unlock;
109 }
110
111 oldflags = ocfs2_inode->ip_attr;
112 flags = flags & mask;
113 flags |= oldflags & ~mask;
114
115 /*
116 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
117 * the relevant capability.
118 */
119 status = -EPERM;
120 if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
121 (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
122 if (!capable(CAP_LINUX_IMMUTABLE))
123 goto bail_unlock;
124 }
125
126 ocfs2_inode->ip_attr = flags;
127 ocfs2_set_inode_flags(inode);
128
129 status = ocfs2_mark_inode_dirty(handle, inode, bh);
130 if (status < 0)
131 mlog_errno(status);
132
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700133 ocfs2_commit_trans(osb, handle);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700134bail_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700135 ocfs2_inode_unlock(inode, 1);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700136bail:
137 mutex_unlock(&inode->i_mutex);
138
Mark Fasheha81cb882008-10-07 14:25:16 -0700139 brelse(bh);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700140
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700141 return status;
142}
143
Tristan Yeddee5cd2010-05-22 16:26:33 +0800144int ocfs2_info_handle_blocksize(struct inode *inode,
145 struct ocfs2_info_request __user *req)
146{
147 int status = -EFAULT;
148 struct ocfs2_info_blocksize oib;
149
150 if (o2info_from_user(oib, req))
151 goto bail;
152
153 oib.ib_blocksize = inode->i_sb->s_blocksize;
Tristan Ye1936a262011-01-30 14:25:59 +0800154
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800155 o2info_set_request_filled(&oib.ib_req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800156
157 if (o2info_to_user(oib, req))
158 goto bail;
159
160 status = 0;
161bail:
162 if (status)
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800163 o2info_set_request_error(&oib.ib_req, req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800164
165 return status;
166}
167
168int ocfs2_info_handle_clustersize(struct inode *inode,
169 struct ocfs2_info_request __user *req)
170{
171 int status = -EFAULT;
172 struct ocfs2_info_clustersize oic;
173 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
174
175 if (o2info_from_user(oic, req))
176 goto bail;
177
178 oic.ic_clustersize = osb->s_clustersize;
Tristan Ye1936a262011-01-30 14:25:59 +0800179
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800180 o2info_set_request_filled(&oic.ic_req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800181
182 if (o2info_to_user(oic, req))
183 goto bail;
184
185 status = 0;
186bail:
187 if (status)
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800188 o2info_set_request_error(&oic.ic_req, req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800189
190 return status;
191}
192
193int ocfs2_info_handle_maxslots(struct inode *inode,
194 struct ocfs2_info_request __user *req)
195{
196 int status = -EFAULT;
197 struct ocfs2_info_maxslots oim;
198 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
199
200 if (o2info_from_user(oim, req))
201 goto bail;
202
203 oim.im_max_slots = osb->max_slots;
Tristan Ye1936a262011-01-30 14:25:59 +0800204
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800205 o2info_set_request_filled(&oim.im_req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800206
207 if (o2info_to_user(oim, req))
208 goto bail;
209
210 status = 0;
211bail:
212 if (status)
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800213 o2info_set_request_error(&oim.im_req, req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800214
215 return status;
216}
217
218int ocfs2_info_handle_label(struct inode *inode,
219 struct ocfs2_info_request __user *req)
220{
221 int status = -EFAULT;
222 struct ocfs2_info_label oil;
223 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
224
225 if (o2info_from_user(oil, req))
226 goto bail;
227
228 memcpy(oil.il_label, osb->vol_label, OCFS2_MAX_VOL_LABEL_LEN);
Tristan Ye1936a262011-01-30 14:25:59 +0800229
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800230 o2info_set_request_filled(&oil.il_req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800231
232 if (o2info_to_user(oil, req))
233 goto bail;
234
235 status = 0;
236bail:
237 if (status)
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800238 o2info_set_request_error(&oil.il_req, req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800239
240 return status;
241}
242
243int ocfs2_info_handle_uuid(struct inode *inode,
244 struct ocfs2_info_request __user *req)
245{
246 int status = -EFAULT;
247 struct ocfs2_info_uuid oiu;
248 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
249
250 if (o2info_from_user(oiu, req))
251 goto bail;
252
253 memcpy(oiu.iu_uuid_str, osb->uuid_str, OCFS2_TEXT_UUID_LEN + 1);
Tristan Ye1936a262011-01-30 14:25:59 +0800254
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800255 o2info_set_request_filled(&oiu.iu_req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800256
257 if (o2info_to_user(oiu, req))
258 goto bail;
259
260 status = 0;
261bail:
262 if (status)
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800263 o2info_set_request_error(&oiu.iu_req, req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800264
265 return status;
266}
267
268int ocfs2_info_handle_fs_features(struct inode *inode,
269 struct ocfs2_info_request __user *req)
270{
271 int status = -EFAULT;
272 struct ocfs2_info_fs_features oif;
273 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
274
275 if (o2info_from_user(oif, req))
276 goto bail;
277
278 oif.if_compat_features = osb->s_feature_compat;
279 oif.if_incompat_features = osb->s_feature_incompat;
280 oif.if_ro_compat_features = osb->s_feature_ro_compat;
Tristan Ye1936a262011-01-30 14:25:59 +0800281
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800282 o2info_set_request_filled(&oif.if_req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800283
284 if (o2info_to_user(oif, req))
285 goto bail;
286
287 status = 0;
288bail:
289 if (status)
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800290 o2info_set_request_error(&oif.if_req, req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800291
292 return status;
293}
294
295int ocfs2_info_handle_journal_size(struct inode *inode,
296 struct ocfs2_info_request __user *req)
297{
298 int status = -EFAULT;
299 struct ocfs2_info_journal_size oij;
300 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
301
302 if (o2info_from_user(oij, req))
303 goto bail;
304
305 oij.ij_journal_size = osb->journal->j_inode->i_size;
306
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800307 o2info_set_request_filled(&oij.ij_req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800308
309 if (o2info_to_user(oij, req))
310 goto bail;
311
312 status = 0;
313bail:
314 if (status)
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800315 o2info_set_request_error(&oij.ij_req, req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800316
317 return status;
318}
319
Tristan Ye3e5db172011-05-24 15:25:54 +0800320int ocfs2_info_scan_inode_alloc(struct ocfs2_super *osb,
321 struct inode *inode_alloc, u64 blkno,
322 struct ocfs2_info_freeinode *fi, u32 slot)
323{
324 int status = 0, unlock = 0;
325
326 struct buffer_head *bh = NULL;
327 struct ocfs2_dinode *dinode_alloc = NULL;
328
329 if (inode_alloc)
330 mutex_lock(&inode_alloc->i_mutex);
331
332 if (o2info_coherent(&fi->ifi_req)) {
333 status = ocfs2_inode_lock(inode_alloc, &bh, 0);
334 if (status < 0) {
335 mlog_errno(status);
336 goto bail;
337 }
338 unlock = 1;
339 } else {
340 status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
341 if (status < 0) {
342 mlog_errno(status);
343 goto bail;
344 }
345 }
346
347 dinode_alloc = (struct ocfs2_dinode *)bh->b_data;
348
349 fi->ifi_stat[slot].lfi_total =
350 le32_to_cpu(dinode_alloc->id1.bitmap1.i_total);
351 fi->ifi_stat[slot].lfi_free =
352 le32_to_cpu(dinode_alloc->id1.bitmap1.i_total) -
353 le32_to_cpu(dinode_alloc->id1.bitmap1.i_used);
354
355bail:
356 if (unlock)
357 ocfs2_inode_unlock(inode_alloc, 0);
358
359 if (inode_alloc)
360 mutex_unlock(&inode_alloc->i_mutex);
361
362 brelse(bh);
363
364 return status;
365}
366
367int ocfs2_info_handle_freeinode(struct inode *inode,
368 struct ocfs2_info_request __user *req)
369{
370 u32 i;
371 u64 blkno = -1;
372 char namebuf[40];
373 int status = -EFAULT, type = INODE_ALLOC_SYSTEM_INODE;
374 struct ocfs2_info_freeinode *oifi = NULL;
375 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
376 struct inode *inode_alloc = NULL;
377
378 oifi = kzalloc(sizeof(struct ocfs2_info_freeinode), GFP_KERNEL);
379 if (!oifi) {
380 status = -ENOMEM;
381 mlog_errno(status);
382 goto bail;
383 }
384
385 if (o2info_from_user(*oifi, req))
386 goto bail;
387
388 oifi->ifi_slotnum = osb->max_slots;
389
390 for (i = 0; i < oifi->ifi_slotnum; i++) {
391 if (o2info_coherent(&oifi->ifi_req)) {
392 inode_alloc = ocfs2_get_system_file_inode(osb, type, i);
393 if (!inode_alloc) {
394 mlog(ML_ERROR, "unable to get alloc inode in "
395 "slot %u\n", i);
396 status = -EIO;
397 goto bail;
398 }
399 } else {
400 ocfs2_sprintf_system_inode_name(namebuf,
401 sizeof(namebuf),
402 type, i);
403 status = ocfs2_lookup_ino_from_name(osb->sys_root_inode,
404 namebuf,
405 strlen(namebuf),
406 &blkno);
407 if (status < 0) {
408 status = -ENOENT;
409 goto bail;
410 }
411 }
412
413 status = ocfs2_info_scan_inode_alloc(osb, inode_alloc, blkno, oifi, i);
414 if (status < 0)
415 goto bail;
416
417 iput(inode_alloc);
418 inode_alloc = NULL;
419 }
420
421 o2info_set_request_filled(&oifi->ifi_req);
422
423 if (o2info_to_user(*oifi, req))
424 goto bail;
425
426 status = 0;
427bail:
428 if (status)
429 o2info_set_request_error(&oifi->ifi_req, req);
430
431 kfree(oifi);
432
433 return status;
434}
435
Tristan Yeddee5cd2010-05-22 16:26:33 +0800436int ocfs2_info_handle_unknown(struct inode *inode,
437 struct ocfs2_info_request __user *req)
438{
439 int status = -EFAULT;
440 struct ocfs2_info_request oir;
441
442 if (o2info_from_user(oir, req))
443 goto bail;
444
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800445 o2info_clear_request_filled(&oir);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800446
447 if (o2info_to_user(oir, req))
448 goto bail;
449
450 status = 0;
451bail:
452 if (status)
Tristan Ye8aa1fa32011-05-24 15:22:59 +0800453 o2info_set_request_error(&oir, req);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800454
455 return status;
456}
457
458/*
459 * Validate and distinguish OCFS2_IOC_INFO requests.
460 *
461 * - validate the magic number.
462 * - distinguish different requests.
463 * - validate size of different requests.
464 */
465int ocfs2_info_handle_request(struct inode *inode,
466 struct ocfs2_info_request __user *req)
467{
468 int status = -EFAULT;
469 struct ocfs2_info_request oir;
470
471 if (o2info_from_user(oir, req))
472 goto bail;
473
474 status = -EINVAL;
475 if (oir.ir_magic != OCFS2_INFO_MAGIC)
476 goto bail;
477
478 switch (oir.ir_code) {
479 case OCFS2_INFO_BLOCKSIZE:
480 if (oir.ir_size == sizeof(struct ocfs2_info_blocksize))
481 status = ocfs2_info_handle_blocksize(inode, req);
482 break;
483 case OCFS2_INFO_CLUSTERSIZE:
484 if (oir.ir_size == sizeof(struct ocfs2_info_clustersize))
485 status = ocfs2_info_handle_clustersize(inode, req);
486 break;
487 case OCFS2_INFO_MAXSLOTS:
488 if (oir.ir_size == sizeof(struct ocfs2_info_maxslots))
489 status = ocfs2_info_handle_maxslots(inode, req);
490 break;
491 case OCFS2_INFO_LABEL:
492 if (oir.ir_size == sizeof(struct ocfs2_info_label))
493 status = ocfs2_info_handle_label(inode, req);
494 break;
495 case OCFS2_INFO_UUID:
496 if (oir.ir_size == sizeof(struct ocfs2_info_uuid))
497 status = ocfs2_info_handle_uuid(inode, req);
498 break;
499 case OCFS2_INFO_FS_FEATURES:
500 if (oir.ir_size == sizeof(struct ocfs2_info_fs_features))
501 status = ocfs2_info_handle_fs_features(inode, req);
502 break;
503 case OCFS2_INFO_JOURNAL_SIZE:
504 if (oir.ir_size == sizeof(struct ocfs2_info_journal_size))
505 status = ocfs2_info_handle_journal_size(inode, req);
506 break;
Tristan Ye3e5db172011-05-24 15:25:54 +0800507 case OCFS2_INFO_FREEINODE:
508 if (oir.ir_size == sizeof(struct ocfs2_info_freeinode))
509 status = ocfs2_info_handle_freeinode(inode, req);
510 break;
Tristan Yeddee5cd2010-05-22 16:26:33 +0800511 default:
512 status = ocfs2_info_handle_unknown(inode, req);
513 break;
514 }
515
516bail:
517 return status;
518}
519
520int ocfs2_get_request_ptr(struct ocfs2_info *info, int idx,
521 u64 *req_addr, int compat_flag)
522{
523 int status = -EFAULT;
524 u64 __user *bp = NULL;
525
526 if (compat_flag) {
527#ifdef CONFIG_COMPAT
528 /*
529 * pointer bp stores the base address of a pointers array,
530 * which collects all addresses of separate request.
531 */
532 bp = (u64 __user *)(unsigned long)compat_ptr(info->oi_requests);
533#else
534 BUG();
535#endif
536 } else
537 bp = (u64 __user *)(unsigned long)(info->oi_requests);
538
539 if (o2info_from_user(*req_addr, bp + idx))
540 goto bail;
541
542 status = 0;
543bail:
544 return status;
545}
546
547/*
548 * OCFS2_IOC_INFO handles an array of requests passed from userspace.
549 *
550 * ocfs2_info_handle() recevies a large info aggregation, grab and
551 * validate the request count from header, then break it into small
552 * pieces, later specific handlers can handle them one by one.
553 *
554 * Idea here is to make each separate request small enough to ensure
555 * a better backward&forward compatibility, since a small piece of
556 * request will be less likely to be broken if disk layout get changed.
557 */
558int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info,
559 int compat_flag)
560{
561 int i, status = 0;
562 u64 req_addr;
563 struct ocfs2_info_request __user *reqp;
564
565 if ((info->oi_count > OCFS2_INFO_MAX_REQUEST) ||
566 (!info->oi_requests)) {
567 status = -EINVAL;
568 goto bail;
569 }
570
571 for (i = 0; i < info->oi_count; i++) {
572
573 status = ocfs2_get_request_ptr(info, i, &req_addr, compat_flag);
574 if (status)
575 break;
576
577 reqp = (struct ocfs2_info_request *)(unsigned long)req_addr;
578 if (!reqp) {
579 status = -EINVAL;
580 goto bail;
581 }
582
583 status = ocfs2_info_handle_request(inode, reqp);
584 if (status)
585 break;
586 }
587
588bail:
589 return status;
590}
591
Andi Kleenc9ec1482008-01-27 03:17:17 +0100592long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700593{
Andi Kleenc9ec1482008-01-27 03:17:17 +0100594 struct inode *inode = filp->f_path.dentry->d_inode;
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700595 unsigned int flags;
Tao Mad6590722007-12-18 15:47:03 +0800596 int new_clusters;
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700597 int status;
Mark Fashehb2580102007-03-09 16:53:21 -0800598 struct ocfs2_space_resv sr;
Tao Ma7909f2b2007-12-18 15:47:25 +0800599 struct ocfs2_new_group_input input;
Tao Mabd508732009-09-21 11:25:14 +0800600 struct reflink_arguments args;
601 const char *old_path, *new_path;
602 bool preserve;
Tristan Yeddee5cd2010-05-22 16:26:33 +0800603 struct ocfs2_info info;
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700604
605 switch (cmd) {
606 case OCFS2_IOC_GETFLAGS:
607 status = ocfs2_get_inode_attr(inode, &flags);
608 if (status < 0)
609 return status;
610
611 flags &= OCFS2_FL_VISIBLE;
612 return put_user(flags, (int __user *) arg);
613 case OCFS2_IOC_SETFLAGS:
614 if (get_user(flags, (int __user *) arg))
615 return -EFAULT;
616
Dave Hansen42a74f22008-02-15 14:37:46 -0800617 status = mnt_want_write(filp->f_path.mnt);
618 if (status)
619 return status;
620 status = ocfs2_set_inode_attr(inode, flags,
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700621 OCFS2_FL_MODIFIABLE);
Dave Hansen42a74f22008-02-15 14:37:46 -0800622 mnt_drop_write(filp->f_path.mnt);
623 return status;
Mark Fashehb2580102007-03-09 16:53:21 -0800624 case OCFS2_IOC_RESVSP:
625 case OCFS2_IOC_RESVSP64:
626 case OCFS2_IOC_UNRESVSP:
627 case OCFS2_IOC_UNRESVSP64:
628 if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
629 return -EFAULT;
630
631 return ocfs2_change_file_space(filp, cmd, &sr);
Tao Mad6590722007-12-18 15:47:03 +0800632 case OCFS2_IOC_GROUP_EXTEND:
Mark Fasheh0957f002007-12-18 18:58:18 -0800633 if (!capable(CAP_SYS_RESOURCE))
634 return -EPERM;
635
Tao Mad6590722007-12-18 15:47:03 +0800636 if (get_user(new_clusters, (int __user *)arg))
637 return -EFAULT;
638
639 return ocfs2_group_extend(inode, new_clusters);
Tao Ma7909f2b2007-12-18 15:47:25 +0800640 case OCFS2_IOC_GROUP_ADD:
641 case OCFS2_IOC_GROUP_ADD64:
Mark Fasheh0957f002007-12-18 18:58:18 -0800642 if (!capable(CAP_SYS_RESOURCE))
643 return -EPERM;
644
Tao Ma7909f2b2007-12-18 15:47:25 +0800645 if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
646 return -EFAULT;
647
648 return ocfs2_group_add(inode, &input);
Tao Mabd508732009-09-21 11:25:14 +0800649 case OCFS2_IOC_REFLINK:
650 if (copy_from_user(&args, (struct reflink_arguments *)arg,
651 sizeof(args)))
652 return -EFAULT;
653 old_path = (const char *)(unsigned long)args.old_path;
654 new_path = (const char *)(unsigned long)args.new_path;
655 preserve = (args.preserve != 0);
656
657 return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800658 case OCFS2_IOC_INFO:
659 if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
660 sizeof(struct ocfs2_info)))
661 return -EFAULT;
662
663 return ocfs2_info_handle(inode, &info, 0);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700664 default:
665 return -ENOTTY;
666 }
667}
668
Mark Fasheh586d2322007-03-09 15:56:28 -0800669#ifdef CONFIG_COMPAT
670long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
671{
Tao Ma34e6c592010-01-27 10:21:52 +0800672 bool preserve;
673 struct reflink_arguments args;
674 struct inode *inode = file->f_path.dentry->d_inode;
Tristan Yeddee5cd2010-05-22 16:26:33 +0800675 struct ocfs2_info info;
Tao Ma34e6c592010-01-27 10:21:52 +0800676
Mark Fasheh586d2322007-03-09 15:56:28 -0800677 switch (cmd) {
678 case OCFS2_IOC32_GETFLAGS:
679 cmd = OCFS2_IOC_GETFLAGS;
680 break;
681 case OCFS2_IOC32_SETFLAGS:
682 cmd = OCFS2_IOC_SETFLAGS;
683 break;
Mark Fashehb2580102007-03-09 16:53:21 -0800684 case OCFS2_IOC_RESVSP:
685 case OCFS2_IOC_RESVSP64:
686 case OCFS2_IOC_UNRESVSP:
687 case OCFS2_IOC_UNRESVSP64:
Tao Mad6590722007-12-18 15:47:03 +0800688 case OCFS2_IOC_GROUP_EXTEND:
Tao Ma7909f2b2007-12-18 15:47:25 +0800689 case OCFS2_IOC_GROUP_ADD:
690 case OCFS2_IOC_GROUP_ADD64:
Mark Fashehb2580102007-03-09 16:53:21 -0800691 break;
Tao Ma34e6c592010-01-27 10:21:52 +0800692 case OCFS2_IOC_REFLINK:
693 if (copy_from_user(&args, (struct reflink_arguments *)arg,
694 sizeof(args)))
695 return -EFAULT;
696 preserve = (args.preserve != 0);
697
698 return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
699 compat_ptr(args.new_path), preserve);
Tristan Yeddee5cd2010-05-22 16:26:33 +0800700 case OCFS2_IOC_INFO:
701 if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
702 sizeof(struct ocfs2_info)))
703 return -EFAULT;
704
705 return ocfs2_info_handle(inode, &info, 1);
Mark Fasheh586d2322007-03-09 15:56:28 -0800706 default:
707 return -ENOIOCTLCMD;
708 }
709
Andi Kleenc9ec1482008-01-27 03:17:17 +0100710 return ocfs2_ioctl(file, cmd, arg);
Mark Fasheh586d2322007-03-09 15:56:28 -0800711}
712#endif