xfs_ioctl.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_alloc.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_ioctl.h"
  32. #include "xfs_rtalloc.h"
  33. #include "xfs_itable.h"
  34. #include "xfs_error.h"
  35. #include "xfs_attr.h"
  36. #include "xfs_bmap.h"
  37. #include "xfs_buf_item.h"
  38. #include "xfs_utils.h"
  39. #include "xfs_dfrag.h"
  40. #include "xfs_fsops.h"
  41. #include "xfs_vnodeops.h"
  42. #include "xfs_quota.h"
  43. #include "xfs_inode_item.h"
  44. #include "xfs_export.h"
  45. #include "xfs_trace.h"
  46. #include <linux/capability.h>
  47. #include <linux/dcache.h>
  48. #include <linux/mount.h>
  49. #include <linux/namei.h>
  50. #include <linux/pagemap.h>
  51. #include <linux/slab.h>
  52. #include <linux/exportfs.h>
  53. /*
  54. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  55. * a file or fs handle.
  56. *
  57. * XFS_IOC_PATH_TO_FSHANDLE
  58. * returns fs handle for a mount point or path within that mount point
  59. * XFS_IOC_FD_TO_HANDLE
  60. * returns full handle for a FD opened in user space
  61. * XFS_IOC_PATH_TO_HANDLE
  62. * returns full handle for a path
  63. */
  64. int
  65. xfs_find_handle(
  66. unsigned int cmd,
  67. xfs_fsop_handlereq_t *hreq)
  68. {
  69. int hsize;
  70. xfs_handle_t handle;
  71. struct inode *inode;
  72. struct file *file = NULL;
  73. struct path path;
  74. int error;
  75. struct xfs_inode *ip;
  76. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  77. file = fget(hreq->fd);
  78. if (!file)
  79. return -EBADF;
  80. inode = file->f_path.dentry->d_inode;
  81. } else {
  82. error = user_lpath((const char __user *)hreq->path, &path);
  83. if (error)
  84. return error;
  85. inode = path.dentry->d_inode;
  86. }
  87. ip = XFS_I(inode);
  88. /*
  89. * We can only generate handles for inodes residing on a XFS filesystem,
  90. * and only for regular files, directories or symbolic links.
  91. */
  92. error = -EINVAL;
  93. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  94. goto out_put;
  95. error = -EBADF;
  96. if (!S_ISREG(inode->i_mode) &&
  97. !S_ISDIR(inode->i_mode) &&
  98. !S_ISLNK(inode->i_mode))
  99. goto out_put;
  100. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  101. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  102. /*
  103. * This handle only contains an fsid, zero the rest.
  104. */
  105. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  106. hsize = sizeof(xfs_fsid_t);
  107. } else {
  108. int lock_mode;
  109. lock_mode = xfs_ilock_map_shared(ip);
  110. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  111. sizeof(handle.ha_fid.fid_len);
  112. handle.ha_fid.fid_pad = 0;
  113. handle.ha_fid.fid_gen = ip->i_d.di_gen;
  114. handle.ha_fid.fid_ino = ip->i_ino;
  115. xfs_iunlock_map_shared(ip, lock_mode);
  116. hsize = XFS_HSIZE(handle);
  117. }
  118. error = -EFAULT;
  119. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  120. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  121. goto out_put;
  122. error = 0;
  123. out_put:
  124. if (cmd == XFS_IOC_FD_TO_HANDLE)
  125. fput(file);
  126. else
  127. path_put(&path);
  128. return error;
  129. }
  130. /*
  131. * No need to do permission checks on the various pathname components
  132. * as the handle operations are privileged.
  133. */
  134. STATIC int
  135. xfs_handle_acceptable(
  136. void *context,
  137. struct dentry *dentry)
  138. {
  139. return 1;
  140. }
  141. /*
  142. * Convert userspace handle data into a dentry.
  143. */
  144. struct dentry *
  145. xfs_handle_to_dentry(
  146. struct file *parfilp,
  147. void __user *uhandle,
  148. u32 hlen)
  149. {
  150. xfs_handle_t handle;
  151. struct xfs_fid64 fid;
  152. /*
  153. * Only allow handle opens under a directory.
  154. */
  155. if (!S_ISDIR(parfilp->f_path.dentry->d_inode->i_mode))
  156. return ERR_PTR(-ENOTDIR);
  157. if (hlen != sizeof(xfs_handle_t))
  158. return ERR_PTR(-EINVAL);
  159. if (copy_from_user(&handle, uhandle, hlen))
  160. return ERR_PTR(-EFAULT);
  161. if (handle.ha_fid.fid_len !=
  162. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  163. return ERR_PTR(-EINVAL);
  164. memset(&fid, 0, sizeof(struct fid));
  165. fid.ino = handle.ha_fid.fid_ino;
  166. fid.gen = handle.ha_fid.fid_gen;
  167. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  168. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  169. xfs_handle_acceptable, NULL);
  170. }
  171. STATIC struct dentry *
  172. xfs_handlereq_to_dentry(
  173. struct file *parfilp,
  174. xfs_fsop_handlereq_t *hreq)
  175. {
  176. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  177. }
  178. int
  179. xfs_open_by_handle(
  180. struct file *parfilp,
  181. xfs_fsop_handlereq_t *hreq)
  182. {
  183. const struct cred *cred = current_cred();
  184. int error;
  185. int fd;
  186. int permflag;
  187. struct file *filp;
  188. struct inode *inode;
  189. struct dentry *dentry;
  190. if (!capable(CAP_SYS_ADMIN))
  191. return -XFS_ERROR(EPERM);
  192. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  193. if (IS_ERR(dentry))
  194. return PTR_ERR(dentry);
  195. inode = dentry->d_inode;
  196. /* Restrict xfs_open_by_handle to directories & regular files. */
  197. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  198. error = -XFS_ERROR(EPERM);
  199. goto out_dput;
  200. }
  201. #if BITS_PER_LONG != 32
  202. hreq->oflags |= O_LARGEFILE;
  203. #endif
  204. /* Put open permission in namei format. */
  205. permflag = hreq->oflags;
  206. if ((permflag+1) & O_ACCMODE)
  207. permflag++;
  208. if (permflag & O_TRUNC)
  209. permflag |= 2;
  210. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  211. (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
  212. error = -XFS_ERROR(EPERM);
  213. goto out_dput;
  214. }
  215. if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  216. error = -XFS_ERROR(EACCES);
  217. goto out_dput;
  218. }
  219. /* Can't write directories. */
  220. if (S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
  221. error = -XFS_ERROR(EISDIR);
  222. goto out_dput;
  223. }
  224. fd = get_unused_fd();
  225. if (fd < 0) {
  226. error = fd;
  227. goto out_dput;
  228. }
  229. filp = dentry_open(dentry, mntget(parfilp->f_path.mnt),
  230. hreq->oflags, cred);
  231. if (IS_ERR(filp)) {
  232. put_unused_fd(fd);
  233. return PTR_ERR(filp);
  234. }
  235. if (inode->i_mode & S_IFREG) {
  236. filp->f_flags |= O_NOATIME;
  237. filp->f_mode |= FMODE_NOCMTIME;
  238. }
  239. fd_install(fd, filp);
  240. return fd;
  241. out_dput:
  242. dput(dentry);
  243. return error;
  244. }
  245. /*
  246. * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
  247. * unused first argument.
  248. */
  249. STATIC int
  250. do_readlink(
  251. char __user *buffer,
  252. int buflen,
  253. const char *link)
  254. {
  255. int len;
  256. len = PTR_ERR(link);
  257. if (IS_ERR(link))
  258. goto out;
  259. len = strlen(link);
  260. if (len > (unsigned) buflen)
  261. len = buflen;
  262. if (copy_to_user(buffer, link, len))
  263. len = -EFAULT;
  264. out:
  265. return len;
  266. }
  267. int
  268. xfs_readlink_by_handle(
  269. struct file *parfilp,
  270. xfs_fsop_handlereq_t *hreq)
  271. {
  272. struct dentry *dentry;
  273. __u32 olen;
  274. void *link;
  275. int error;
  276. if (!capable(CAP_SYS_ADMIN))
  277. return -XFS_ERROR(EPERM);
  278. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  279. if (IS_ERR(dentry))
  280. return PTR_ERR(dentry);
  281. /* Restrict this handle operation to symlinks only. */
  282. if (!S_ISLNK(dentry->d_inode->i_mode)) {
  283. error = -XFS_ERROR(EINVAL);
  284. goto out_dput;
  285. }
  286. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  287. error = -XFS_ERROR(EFAULT);
  288. goto out_dput;
  289. }
  290. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  291. if (!link) {
  292. error = -XFS_ERROR(ENOMEM);
  293. goto out_dput;
  294. }
  295. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  296. if (error)
  297. goto out_kfree;
  298. error = do_readlink(hreq->ohandle, olen, link);
  299. if (error)
  300. goto out_kfree;
  301. out_kfree:
  302. kfree(link);
  303. out_dput:
  304. dput(dentry);
  305. return error;
  306. }
  307. STATIC int
  308. xfs_fssetdm_by_handle(
  309. struct file *parfilp,
  310. void __user *arg)
  311. {
  312. int error;
  313. struct fsdmidata fsd;
  314. xfs_fsop_setdm_handlereq_t dmhreq;
  315. struct dentry *dentry;
  316. if (!capable(CAP_MKNOD))
  317. return -XFS_ERROR(EPERM);
  318. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  319. return -XFS_ERROR(EFAULT);
  320. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  321. if (IS_ERR(dentry))
  322. return PTR_ERR(dentry);
  323. if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
  324. error = -XFS_ERROR(EPERM);
  325. goto out;
  326. }
  327. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  328. error = -XFS_ERROR(EFAULT);
  329. goto out;
  330. }
  331. error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
  332. fsd.fsd_dmstate);
  333. out:
  334. dput(dentry);
  335. return error;
  336. }
  337. STATIC int
  338. xfs_attrlist_by_handle(
  339. struct file *parfilp,
  340. void __user *arg)
  341. {
  342. int error = -ENOMEM;
  343. attrlist_cursor_kern_t *cursor;
  344. xfs_fsop_attrlist_handlereq_t al_hreq;
  345. struct dentry *dentry;
  346. char *kbuf;
  347. if (!capable(CAP_SYS_ADMIN))
  348. return -XFS_ERROR(EPERM);
  349. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  350. return -XFS_ERROR(EFAULT);
  351. if (al_hreq.buflen > XATTR_LIST_MAX)
  352. return -XFS_ERROR(EINVAL);
  353. /*
  354. * Reject flags, only allow namespaces.
  355. */
  356. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  357. return -XFS_ERROR(EINVAL);
  358. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  359. if (IS_ERR(dentry))
  360. return PTR_ERR(dentry);
  361. kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
  362. if (!kbuf)
  363. goto out_dput;
  364. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  365. error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
  366. al_hreq.flags, cursor);
  367. if (error)
  368. goto out_kfree;
  369. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  370. error = -EFAULT;
  371. out_kfree:
  372. kfree(kbuf);
  373. out_dput:
  374. dput(dentry);
  375. return error;
  376. }
  377. int
  378. xfs_attrmulti_attr_get(
  379. struct inode *inode,
  380. unsigned char *name,
  381. unsigned char __user *ubuf,
  382. __uint32_t *len,
  383. __uint32_t flags)
  384. {
  385. unsigned char *kbuf;
  386. int error = EFAULT;
  387. if (*len > XATTR_SIZE_MAX)
  388. return EINVAL;
  389. kbuf = kmalloc(*len, GFP_KERNEL);
  390. if (!kbuf)
  391. return ENOMEM;
  392. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  393. if (error)
  394. goto out_kfree;
  395. if (copy_to_user(ubuf, kbuf, *len))
  396. error = EFAULT;
  397. out_kfree:
  398. kfree(kbuf);
  399. return error;
  400. }
  401. int
  402. xfs_attrmulti_attr_set(
  403. struct inode *inode,
  404. unsigned char *name,
  405. const unsigned char __user *ubuf,
  406. __uint32_t len,
  407. __uint32_t flags)
  408. {
  409. unsigned char *kbuf;
  410. int error = EFAULT;
  411. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  412. return EPERM;
  413. if (len > XATTR_SIZE_MAX)
  414. return EINVAL;
  415. kbuf = memdup_user(ubuf, len);
  416. if (IS_ERR(kbuf))
  417. return PTR_ERR(kbuf);
  418. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  419. return error;
  420. }
  421. int
  422. xfs_attrmulti_attr_remove(
  423. struct inode *inode,
  424. unsigned char *name,
  425. __uint32_t flags)
  426. {
  427. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  428. return EPERM;
  429. return xfs_attr_remove(XFS_I(inode), name, flags);
  430. }
  431. STATIC int
  432. xfs_attrmulti_by_handle(
  433. struct file *parfilp,
  434. void __user *arg)
  435. {
  436. int error;
  437. xfs_attr_multiop_t *ops;
  438. xfs_fsop_attrmulti_handlereq_t am_hreq;
  439. struct dentry *dentry;
  440. unsigned int i, size;
  441. unsigned char *attr_name;
  442. if (!capable(CAP_SYS_ADMIN))
  443. return -XFS_ERROR(EPERM);
  444. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  445. return -XFS_ERROR(EFAULT);
  446. /* overflow check */
  447. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  448. return -E2BIG;
  449. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  450. if (IS_ERR(dentry))
  451. return PTR_ERR(dentry);
  452. error = E2BIG;
  453. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  454. if (!size || size > 16 * PAGE_SIZE)
  455. goto out_dput;
  456. ops = memdup_user(am_hreq.ops, size);
  457. if (IS_ERR(ops)) {
  458. error = PTR_ERR(ops);
  459. goto out_dput;
  460. }
  461. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  462. if (!attr_name)
  463. goto out_kfree_ops;
  464. error = 0;
  465. for (i = 0; i < am_hreq.opcount; i++) {
  466. ops[i].am_error = strncpy_from_user((char *)attr_name,
  467. ops[i].am_attrname, MAXNAMELEN);
  468. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  469. error = -ERANGE;
  470. if (ops[i].am_error < 0)
  471. break;
  472. switch (ops[i].am_opcode) {
  473. case ATTR_OP_GET:
  474. ops[i].am_error = xfs_attrmulti_attr_get(
  475. dentry->d_inode, attr_name,
  476. ops[i].am_attrvalue, &ops[i].am_length,
  477. ops[i].am_flags);
  478. break;
  479. case ATTR_OP_SET:
  480. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  481. if (ops[i].am_error)
  482. break;
  483. ops[i].am_error = xfs_attrmulti_attr_set(
  484. dentry->d_inode, attr_name,
  485. ops[i].am_attrvalue, ops[i].am_length,
  486. ops[i].am_flags);
  487. mnt_drop_write(parfilp->f_path.mnt);
  488. break;
  489. case ATTR_OP_REMOVE:
  490. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  491. if (ops[i].am_error)
  492. break;
  493. ops[i].am_error = xfs_attrmulti_attr_remove(
  494. dentry->d_inode, attr_name,
  495. ops[i].am_flags);
  496. mnt_drop_write(parfilp->f_path.mnt);
  497. break;
  498. default:
  499. ops[i].am_error = EINVAL;
  500. }
  501. }
  502. if (copy_to_user(am_hreq.ops, ops, size))
  503. error = XFS_ERROR(EFAULT);
  504. kfree(attr_name);
  505. out_kfree_ops:
  506. kfree(ops);
  507. out_dput:
  508. dput(dentry);
  509. return -error;
  510. }
  511. int
  512. xfs_ioc_space(
  513. struct xfs_inode *ip,
  514. struct inode *inode,
  515. struct file *filp,
  516. int ioflags,
  517. unsigned int cmd,
  518. xfs_flock64_t *bf)
  519. {
  520. int attr_flags = 0;
  521. int error;
  522. /*
  523. * Only allow the sys admin to reserve space unless
  524. * unwritten extents are enabled.
  525. */
  526. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  527. !capable(CAP_SYS_ADMIN))
  528. return -XFS_ERROR(EPERM);
  529. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  530. return -XFS_ERROR(EPERM);
  531. if (!(filp->f_mode & FMODE_WRITE))
  532. return -XFS_ERROR(EBADF);
  533. if (!S_ISREG(inode->i_mode))
  534. return -XFS_ERROR(EINVAL);
  535. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  536. attr_flags |= XFS_ATTR_NONBLOCK;
  537. if (ioflags & IO_INVIS)
  538. attr_flags |= XFS_ATTR_DMI;
  539. error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
  540. return -error;
  541. }
  542. STATIC int
  543. xfs_ioc_bulkstat(
  544. xfs_mount_t *mp,
  545. unsigned int cmd,
  546. void __user *arg)
  547. {
  548. xfs_fsop_bulkreq_t bulkreq;
  549. int count; /* # of records returned */
  550. xfs_ino_t inlast; /* last inode number */
  551. int done;
  552. int error;
  553. /* done = 1 if there are more stats to get and if bulkstat */
  554. /* should be called again (unused here, but used in dmapi) */
  555. if (!capable(CAP_SYS_ADMIN))
  556. return -EPERM;
  557. if (XFS_FORCED_SHUTDOWN(mp))
  558. return -XFS_ERROR(EIO);
  559. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  560. return -XFS_ERROR(EFAULT);
  561. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  562. return -XFS_ERROR(EFAULT);
  563. if ((count = bulkreq.icount) <= 0)
  564. return -XFS_ERROR(EINVAL);
  565. if (bulkreq.ubuffer == NULL)
  566. return -XFS_ERROR(EINVAL);
  567. if (cmd == XFS_IOC_FSINUMBERS)
  568. error = xfs_inumbers(mp, &inlast, &count,
  569. bulkreq.ubuffer, xfs_inumbers_fmt);
  570. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  571. error = xfs_bulkstat_single(mp, &inlast,
  572. bulkreq.ubuffer, &done);
  573. else /* XFS_IOC_FSBULKSTAT */
  574. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  575. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  576. &done);
  577. if (error)
  578. return -error;
  579. if (bulkreq.ocount != NULL) {
  580. if (copy_to_user(bulkreq.lastip, &inlast,
  581. sizeof(xfs_ino_t)))
  582. return -XFS_ERROR(EFAULT);
  583. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  584. return -XFS_ERROR(EFAULT);
  585. }
  586. return 0;
  587. }
  588. STATIC int
  589. xfs_ioc_fsgeometry_v1(
  590. xfs_mount_t *mp,
  591. void __user *arg)
  592. {
  593. xfs_fsop_geom_v1_t fsgeo;
  594. int error;
  595. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  596. if (error)
  597. return -error;
  598. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  599. return -XFS_ERROR(EFAULT);
  600. return 0;
  601. }
  602. STATIC int
  603. xfs_ioc_fsgeometry(
  604. xfs_mount_t *mp,
  605. void __user *arg)
  606. {
  607. xfs_fsop_geom_t fsgeo;
  608. int error;
  609. error = xfs_fs_geometry(mp, &fsgeo, 4);
  610. if (error)
  611. return -error;
  612. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  613. return -XFS_ERROR(EFAULT);
  614. return 0;
  615. }
  616. /*
  617. * Linux extended inode flags interface.
  618. */
  619. STATIC unsigned int
  620. xfs_merge_ioc_xflags(
  621. unsigned int flags,
  622. unsigned int start)
  623. {
  624. unsigned int xflags = start;
  625. if (flags & FS_IMMUTABLE_FL)
  626. xflags |= XFS_XFLAG_IMMUTABLE;
  627. else
  628. xflags &= ~XFS_XFLAG_IMMUTABLE;
  629. if (flags & FS_APPEND_FL)
  630. xflags |= XFS_XFLAG_APPEND;
  631. else
  632. xflags &= ~XFS_XFLAG_APPEND;
  633. if (flags & FS_SYNC_FL)
  634. xflags |= XFS_XFLAG_SYNC;
  635. else
  636. xflags &= ~XFS_XFLAG_SYNC;
  637. if (flags & FS_NOATIME_FL)
  638. xflags |= XFS_XFLAG_NOATIME;
  639. else
  640. xflags &= ~XFS_XFLAG_NOATIME;
  641. if (flags & FS_NODUMP_FL)
  642. xflags |= XFS_XFLAG_NODUMP;
  643. else
  644. xflags &= ~XFS_XFLAG_NODUMP;
  645. return xflags;
  646. }
  647. STATIC unsigned int
  648. xfs_di2lxflags(
  649. __uint16_t di_flags)
  650. {
  651. unsigned int flags = 0;
  652. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  653. flags |= FS_IMMUTABLE_FL;
  654. if (di_flags & XFS_DIFLAG_APPEND)
  655. flags |= FS_APPEND_FL;
  656. if (di_flags & XFS_DIFLAG_SYNC)
  657. flags |= FS_SYNC_FL;
  658. if (di_flags & XFS_DIFLAG_NOATIME)
  659. flags |= FS_NOATIME_FL;
  660. if (di_flags & XFS_DIFLAG_NODUMP)
  661. flags |= FS_NODUMP_FL;
  662. return flags;
  663. }
  664. STATIC int
  665. xfs_ioc_fsgetxattr(
  666. xfs_inode_t *ip,
  667. int attr,
  668. void __user *arg)
  669. {
  670. struct fsxattr fa;
  671. xfs_ilock(ip, XFS_ILOCK_SHARED);
  672. fa.fsx_xflags = xfs_ip2xflags(ip);
  673. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  674. fa.fsx_projid = ip->i_d.di_projid;
  675. if (attr) {
  676. if (ip->i_afp) {
  677. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  678. fa.fsx_nextents = ip->i_afp->if_bytes /
  679. sizeof(xfs_bmbt_rec_t);
  680. else
  681. fa.fsx_nextents = ip->i_d.di_anextents;
  682. } else
  683. fa.fsx_nextents = 0;
  684. } else {
  685. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  686. fa.fsx_nextents = ip->i_df.if_bytes /
  687. sizeof(xfs_bmbt_rec_t);
  688. else
  689. fa.fsx_nextents = ip->i_d.di_nextents;
  690. }
  691. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  692. if (copy_to_user(arg, &fa, sizeof(fa)))
  693. return -EFAULT;
  694. return 0;
  695. }
  696. STATIC void
  697. xfs_set_diflags(
  698. struct xfs_inode *ip,
  699. unsigned int xflags)
  700. {
  701. unsigned int di_flags;
  702. /* can't set PREALLOC this way, just preserve it */
  703. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  704. if (xflags & XFS_XFLAG_IMMUTABLE)
  705. di_flags |= XFS_DIFLAG_IMMUTABLE;
  706. if (xflags & XFS_XFLAG_APPEND)
  707. di_flags |= XFS_DIFLAG_APPEND;
  708. if (xflags & XFS_XFLAG_SYNC)
  709. di_flags |= XFS_DIFLAG_SYNC;
  710. if (xflags & XFS_XFLAG_NOATIME)
  711. di_flags |= XFS_DIFLAG_NOATIME;
  712. if (xflags & XFS_XFLAG_NODUMP)
  713. di_flags |= XFS_DIFLAG_NODUMP;
  714. if (xflags & XFS_XFLAG_PROJINHERIT)
  715. di_flags |= XFS_DIFLAG_PROJINHERIT;
  716. if (xflags & XFS_XFLAG_NODEFRAG)
  717. di_flags |= XFS_DIFLAG_NODEFRAG;
  718. if (xflags & XFS_XFLAG_FILESTREAM)
  719. di_flags |= XFS_DIFLAG_FILESTREAM;
  720. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  721. if (xflags & XFS_XFLAG_RTINHERIT)
  722. di_flags |= XFS_DIFLAG_RTINHERIT;
  723. if (xflags & XFS_XFLAG_NOSYMLINKS)
  724. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  725. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  726. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  727. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  728. if (xflags & XFS_XFLAG_REALTIME)
  729. di_flags |= XFS_DIFLAG_REALTIME;
  730. if (xflags & XFS_XFLAG_EXTSIZE)
  731. di_flags |= XFS_DIFLAG_EXTSIZE;
  732. }
  733. ip->i_d.di_flags = di_flags;
  734. }
  735. STATIC void
  736. xfs_diflags_to_linux(
  737. struct xfs_inode *ip)
  738. {
  739. struct inode *inode = VFS_I(ip);
  740. unsigned int xflags = xfs_ip2xflags(ip);
  741. if (xflags & XFS_XFLAG_IMMUTABLE)
  742. inode->i_flags |= S_IMMUTABLE;
  743. else
  744. inode->i_flags &= ~S_IMMUTABLE;
  745. if (xflags & XFS_XFLAG_APPEND)
  746. inode->i_flags |= S_APPEND;
  747. else
  748. inode->i_flags &= ~S_APPEND;
  749. if (xflags & XFS_XFLAG_SYNC)
  750. inode->i_flags |= S_SYNC;
  751. else
  752. inode->i_flags &= ~S_SYNC;
  753. if (xflags & XFS_XFLAG_NOATIME)
  754. inode->i_flags |= S_NOATIME;
  755. else
  756. inode->i_flags &= ~S_NOATIME;
  757. }
  758. #define FSX_PROJID 1
  759. #define FSX_EXTSIZE 2
  760. #define FSX_XFLAGS 4
  761. #define FSX_NONBLOCK 8
  762. STATIC int
  763. xfs_ioctl_setattr(
  764. xfs_inode_t *ip,
  765. struct fsxattr *fa,
  766. int mask)
  767. {
  768. struct xfs_mount *mp = ip->i_mount;
  769. struct xfs_trans *tp;
  770. unsigned int lock_flags = 0;
  771. struct xfs_dquot *udqp = NULL;
  772. struct xfs_dquot *gdqp = NULL;
  773. struct xfs_dquot *olddquot = NULL;
  774. int code;
  775. trace_xfs_ioctl_setattr(ip);
  776. if (mp->m_flags & XFS_MOUNT_RDONLY)
  777. return XFS_ERROR(EROFS);
  778. if (XFS_FORCED_SHUTDOWN(mp))
  779. return XFS_ERROR(EIO);
  780. /*
  781. * If disk quotas is on, we make sure that the dquots do exist on disk,
  782. * before we start any other transactions. Trying to do this later
  783. * is messy. We don't care to take a readlock to look at the ids
  784. * in inode here, because we can't hold it across the trans_reserve.
  785. * If the IDs do change before we take the ilock, we're covered
  786. * because the i_*dquot fields will get updated anyway.
  787. */
  788. if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
  789. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  790. ip->i_d.di_gid, fa->fsx_projid,
  791. XFS_QMOPT_PQUOTA, &udqp, &gdqp);
  792. if (code)
  793. return code;
  794. }
  795. /*
  796. * For the other attributes, we acquire the inode lock and
  797. * first do an error checking pass.
  798. */
  799. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  800. code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  801. if (code)
  802. goto error_return;
  803. lock_flags = XFS_ILOCK_EXCL;
  804. xfs_ilock(ip, lock_flags);
  805. /*
  806. * CAP_FOWNER overrides the following restrictions:
  807. *
  808. * The user ID of the calling process must be equal
  809. * to the file owner ID, except in cases where the
  810. * CAP_FSETID capability is applicable.
  811. */
  812. if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
  813. code = XFS_ERROR(EPERM);
  814. goto error_return;
  815. }
  816. /*
  817. * Do a quota reservation only if projid is actually going to change.
  818. */
  819. if (mask & FSX_PROJID) {
  820. if (XFS_IS_QUOTA_RUNNING(mp) &&
  821. XFS_IS_PQUOTA_ON(mp) &&
  822. ip->i_d.di_projid != fa->fsx_projid) {
  823. ASSERT(tp);
  824. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  825. capable(CAP_FOWNER) ?
  826. XFS_QMOPT_FORCE_RES : 0);
  827. if (code) /* out of quota */
  828. goto error_return;
  829. }
  830. }
  831. if (mask & FSX_EXTSIZE) {
  832. /*
  833. * Can't change extent size if any extents are allocated.
  834. */
  835. if (ip->i_d.di_nextents &&
  836. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  837. fa->fsx_extsize)) {
  838. code = XFS_ERROR(EINVAL); /* EFBIG? */
  839. goto error_return;
  840. }
  841. /*
  842. * Extent size must be a multiple of the appropriate block
  843. * size, if set at all.
  844. */
  845. if (fa->fsx_extsize != 0) {
  846. xfs_extlen_t size;
  847. if (XFS_IS_REALTIME_INODE(ip) ||
  848. ((mask & FSX_XFLAGS) &&
  849. (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
  850. size = mp->m_sb.sb_rextsize <<
  851. mp->m_sb.sb_blocklog;
  852. } else {
  853. size = mp->m_sb.sb_blocksize;
  854. }
  855. if (fa->fsx_extsize % size) {
  856. code = XFS_ERROR(EINVAL);
  857. goto error_return;
  858. }
  859. }
  860. }
  861. if (mask & FSX_XFLAGS) {
  862. /*
  863. * Can't change realtime flag if any extents are allocated.
  864. */
  865. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  866. (XFS_IS_REALTIME_INODE(ip)) !=
  867. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  868. code = XFS_ERROR(EINVAL); /* EFBIG? */
  869. goto error_return;
  870. }
  871. /*
  872. * If realtime flag is set then must have realtime data.
  873. */
  874. if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  875. if ((mp->m_sb.sb_rblocks == 0) ||
  876. (mp->m_sb.sb_rextsize == 0) ||
  877. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  878. code = XFS_ERROR(EINVAL);
  879. goto error_return;
  880. }
  881. }
  882. /*
  883. * Can't modify an immutable/append-only file unless
  884. * we have appropriate permission.
  885. */
  886. if ((ip->i_d.di_flags &
  887. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  888. (fa->fsx_xflags &
  889. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  890. !capable(CAP_LINUX_IMMUTABLE)) {
  891. code = XFS_ERROR(EPERM);
  892. goto error_return;
  893. }
  894. }
  895. xfs_trans_ijoin(tp, ip);
  896. /*
  897. * Change file ownership. Must be the owner or privileged.
  898. */
  899. if (mask & FSX_PROJID) {
  900. /*
  901. * CAP_FSETID overrides the following restrictions:
  902. *
  903. * The set-user-ID and set-group-ID bits of a file will be
  904. * cleared upon successful return from chown()
  905. */
  906. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  907. !capable(CAP_FSETID))
  908. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  909. /*
  910. * Change the ownerships and register quota modifications
  911. * in the transaction.
  912. */
  913. if (ip->i_d.di_projid != fa->fsx_projid) {
  914. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  915. olddquot = xfs_qm_vop_chown(tp, ip,
  916. &ip->i_gdquot, gdqp);
  917. }
  918. ip->i_d.di_projid = fa->fsx_projid;
  919. /*
  920. * We may have to rev the inode as well as
  921. * the superblock version number since projids didn't
  922. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  923. */
  924. if (ip->i_d.di_version == 1)
  925. xfs_bump_ino_vers2(tp, ip);
  926. }
  927. }
  928. if (mask & FSX_EXTSIZE)
  929. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  930. if (mask & FSX_XFLAGS) {
  931. xfs_set_diflags(ip, fa->fsx_xflags);
  932. xfs_diflags_to_linux(ip);
  933. }
  934. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  935. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  936. XFS_STATS_INC(xs_ig_attrchg);
  937. /*
  938. * If this is a synchronous mount, make sure that the
  939. * transaction goes to disk before returning to the user.
  940. * This is slightly sub-optimal in that truncates require
  941. * two sync transactions instead of one for wsync filesystems.
  942. * One for the truncate and one for the timestamps since we
  943. * don't want to change the timestamps unless we're sure the
  944. * truncate worked. Truncates are less than 1% of the laddis
  945. * mix so this probably isn't worth the trouble to optimize.
  946. */
  947. if (mp->m_flags & XFS_MOUNT_WSYNC)
  948. xfs_trans_set_sync(tp);
  949. code = xfs_trans_commit(tp, 0);
  950. xfs_iunlock(ip, lock_flags);
  951. /*
  952. * Release any dquot(s) the inode had kept before chown.
  953. */
  954. xfs_qm_dqrele(olddquot);
  955. xfs_qm_dqrele(udqp);
  956. xfs_qm_dqrele(gdqp);
  957. return code;
  958. error_return:
  959. xfs_qm_dqrele(udqp);
  960. xfs_qm_dqrele(gdqp);
  961. xfs_trans_cancel(tp, 0);
  962. if (lock_flags)
  963. xfs_iunlock(ip, lock_flags);
  964. return code;
  965. }
  966. STATIC int
  967. xfs_ioc_fssetxattr(
  968. xfs_inode_t *ip,
  969. struct file *filp,
  970. void __user *arg)
  971. {
  972. struct fsxattr fa;
  973. unsigned int mask;
  974. if (copy_from_user(&fa, arg, sizeof(fa)))
  975. return -EFAULT;
  976. mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
  977. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  978. mask |= FSX_NONBLOCK;
  979. return -xfs_ioctl_setattr(ip, &fa, mask);
  980. }
  981. STATIC int
  982. xfs_ioc_getxflags(
  983. xfs_inode_t *ip,
  984. void __user *arg)
  985. {
  986. unsigned int flags;
  987. flags = xfs_di2lxflags(ip->i_d.di_flags);
  988. if (copy_to_user(arg, &flags, sizeof(flags)))
  989. return -EFAULT;
  990. return 0;
  991. }
  992. STATIC int
  993. xfs_ioc_setxflags(
  994. xfs_inode_t *ip,
  995. struct file *filp,
  996. void __user *arg)
  997. {
  998. struct fsxattr fa;
  999. unsigned int flags;
  1000. unsigned int mask;
  1001. if (copy_from_user(&flags, arg, sizeof(flags)))
  1002. return -EFAULT;
  1003. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1004. FS_NOATIME_FL | FS_NODUMP_FL | \
  1005. FS_SYNC_FL))
  1006. return -EOPNOTSUPP;
  1007. mask = FSX_XFLAGS;
  1008. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1009. mask |= FSX_NONBLOCK;
  1010. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1011. return -xfs_ioctl_setattr(ip, &fa, mask);
  1012. }
  1013. STATIC int
  1014. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1015. {
  1016. struct getbmap __user *base = *ap;
  1017. /* copy only getbmap portion (not getbmapx) */
  1018. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1019. return XFS_ERROR(EFAULT);
  1020. *ap += sizeof(struct getbmap);
  1021. return 0;
  1022. }
  1023. STATIC int
  1024. xfs_ioc_getbmap(
  1025. struct xfs_inode *ip,
  1026. int ioflags,
  1027. unsigned int cmd,
  1028. void __user *arg)
  1029. {
  1030. struct getbmapx bmx;
  1031. int error;
  1032. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1033. return -XFS_ERROR(EFAULT);
  1034. if (bmx.bmv_count < 2)
  1035. return -XFS_ERROR(EINVAL);
  1036. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1037. if (ioflags & IO_INVIS)
  1038. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1039. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1040. (struct getbmap *)arg+1);
  1041. if (error)
  1042. return -error;
  1043. /* copy back header - only size of getbmap */
  1044. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1045. return -XFS_ERROR(EFAULT);
  1046. return 0;
  1047. }
  1048. STATIC int
  1049. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1050. {
  1051. struct getbmapx __user *base = *ap;
  1052. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1053. return XFS_ERROR(EFAULT);
  1054. *ap += sizeof(struct getbmapx);
  1055. return 0;
  1056. }
  1057. STATIC int
  1058. xfs_ioc_getbmapx(
  1059. struct xfs_inode *ip,
  1060. void __user *arg)
  1061. {
  1062. struct getbmapx bmx;
  1063. int error;
  1064. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1065. return -XFS_ERROR(EFAULT);
  1066. if (bmx.bmv_count < 2)
  1067. return -XFS_ERROR(EINVAL);
  1068. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1069. return -XFS_ERROR(EINVAL);
  1070. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1071. (struct getbmapx *)arg+1);
  1072. if (error)
  1073. return -error;
  1074. /* copy back header */
  1075. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1076. return -XFS_ERROR(EFAULT);
  1077. return 0;
  1078. }
  1079. /*
  1080. * Note: some of the ioctl's return positive numbers as a
  1081. * byte count indicating success, such as readlink_by_handle.
  1082. * So we don't "sign flip" like most other routines. This means
  1083. * true errors need to be returned as a negative value.
  1084. */
  1085. long
  1086. xfs_file_ioctl(
  1087. struct file *filp,
  1088. unsigned int cmd,
  1089. unsigned long p)
  1090. {
  1091. struct inode *inode = filp->f_path.dentry->d_inode;
  1092. struct xfs_inode *ip = XFS_I(inode);
  1093. struct xfs_mount *mp = ip->i_mount;
  1094. void __user *arg = (void __user *)p;
  1095. int ioflags = 0;
  1096. int error;
  1097. if (filp->f_mode & FMODE_NOCMTIME)
  1098. ioflags |= IO_INVIS;
  1099. trace_xfs_file_ioctl(ip);
  1100. switch (cmd) {
  1101. case XFS_IOC_ALLOCSP:
  1102. case XFS_IOC_FREESP:
  1103. case XFS_IOC_RESVSP:
  1104. case XFS_IOC_UNRESVSP:
  1105. case XFS_IOC_ALLOCSP64:
  1106. case XFS_IOC_FREESP64:
  1107. case XFS_IOC_RESVSP64:
  1108. case XFS_IOC_UNRESVSP64: {
  1109. xfs_flock64_t bf;
  1110. if (copy_from_user(&bf, arg, sizeof(bf)))
  1111. return -XFS_ERROR(EFAULT);
  1112. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1113. }
  1114. case XFS_IOC_DIOINFO: {
  1115. struct dioattr da;
  1116. xfs_buftarg_t *target =
  1117. XFS_IS_REALTIME_INODE(ip) ?
  1118. mp->m_rtdev_targp : mp->m_ddev_targp;
  1119. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  1120. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1121. if (copy_to_user(arg, &da, sizeof(da)))
  1122. return -XFS_ERROR(EFAULT);
  1123. return 0;
  1124. }
  1125. case XFS_IOC_FSBULKSTAT_SINGLE:
  1126. case XFS_IOC_FSBULKSTAT:
  1127. case XFS_IOC_FSINUMBERS:
  1128. return xfs_ioc_bulkstat(mp, cmd, arg);
  1129. case XFS_IOC_FSGEOMETRY_V1:
  1130. return xfs_ioc_fsgeometry_v1(mp, arg);
  1131. case XFS_IOC_FSGEOMETRY:
  1132. return xfs_ioc_fsgeometry(mp, arg);
  1133. case XFS_IOC_GETVERSION:
  1134. return put_user(inode->i_generation, (int __user *)arg);
  1135. case XFS_IOC_FSGETXATTR:
  1136. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1137. case XFS_IOC_FSGETXATTRA:
  1138. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1139. case XFS_IOC_FSSETXATTR:
  1140. return xfs_ioc_fssetxattr(ip, filp, arg);
  1141. case XFS_IOC_GETXFLAGS:
  1142. return xfs_ioc_getxflags(ip, arg);
  1143. case XFS_IOC_SETXFLAGS:
  1144. return xfs_ioc_setxflags(ip, filp, arg);
  1145. case XFS_IOC_FSSETDM: {
  1146. struct fsdmidata dmi;
  1147. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1148. return -XFS_ERROR(EFAULT);
  1149. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1150. dmi.fsd_dmstate);
  1151. return -error;
  1152. }
  1153. case XFS_IOC_GETBMAP:
  1154. case XFS_IOC_GETBMAPA:
  1155. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1156. case XFS_IOC_GETBMAPX:
  1157. return xfs_ioc_getbmapx(ip, arg);
  1158. case XFS_IOC_FD_TO_HANDLE:
  1159. case XFS_IOC_PATH_TO_HANDLE:
  1160. case XFS_IOC_PATH_TO_FSHANDLE: {
  1161. xfs_fsop_handlereq_t hreq;
  1162. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1163. return -XFS_ERROR(EFAULT);
  1164. return xfs_find_handle(cmd, &hreq);
  1165. }
  1166. case XFS_IOC_OPEN_BY_HANDLE: {
  1167. xfs_fsop_handlereq_t hreq;
  1168. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1169. return -XFS_ERROR(EFAULT);
  1170. return xfs_open_by_handle(filp, &hreq);
  1171. }
  1172. case XFS_IOC_FSSETDM_BY_HANDLE:
  1173. return xfs_fssetdm_by_handle(filp, arg);
  1174. case XFS_IOC_READLINK_BY_HANDLE: {
  1175. xfs_fsop_handlereq_t hreq;
  1176. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1177. return -XFS_ERROR(EFAULT);
  1178. return xfs_readlink_by_handle(filp, &hreq);
  1179. }
  1180. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1181. return xfs_attrlist_by_handle(filp, arg);
  1182. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1183. return xfs_attrmulti_by_handle(filp, arg);
  1184. case XFS_IOC_SWAPEXT: {
  1185. struct xfs_swapext sxp;
  1186. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1187. return -XFS_ERROR(EFAULT);
  1188. error = xfs_swapext(&sxp);
  1189. return -error;
  1190. }
  1191. case XFS_IOC_FSCOUNTS: {
  1192. xfs_fsop_counts_t out;
  1193. error = xfs_fs_counts(mp, &out);
  1194. if (error)
  1195. return -error;
  1196. if (copy_to_user(arg, &out, sizeof(out)))
  1197. return -XFS_ERROR(EFAULT);
  1198. return 0;
  1199. }
  1200. case XFS_IOC_SET_RESBLKS: {
  1201. xfs_fsop_resblks_t inout;
  1202. __uint64_t in;
  1203. if (!capable(CAP_SYS_ADMIN))
  1204. return -EPERM;
  1205. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1206. return -XFS_ERROR(EROFS);
  1207. if (copy_from_user(&inout, arg, sizeof(inout)))
  1208. return -XFS_ERROR(EFAULT);
  1209. /* input parameter is passed in resblks field of structure */
  1210. in = inout.resblks;
  1211. error = xfs_reserve_blocks(mp, &in, &inout);
  1212. if (error)
  1213. return -error;
  1214. if (copy_to_user(arg, &inout, sizeof(inout)))
  1215. return -XFS_ERROR(EFAULT);
  1216. return 0;
  1217. }
  1218. case XFS_IOC_GET_RESBLKS: {
  1219. xfs_fsop_resblks_t out;
  1220. if (!capable(CAP_SYS_ADMIN))
  1221. return -EPERM;
  1222. error = xfs_reserve_blocks(mp, NULL, &out);
  1223. if (error)
  1224. return -error;
  1225. if (copy_to_user(arg, &out, sizeof(out)))
  1226. return -XFS_ERROR(EFAULT);
  1227. return 0;
  1228. }
  1229. case XFS_IOC_FSGROWFSDATA: {
  1230. xfs_growfs_data_t in;
  1231. if (copy_from_user(&in, arg, sizeof(in)))
  1232. return -XFS_ERROR(EFAULT);
  1233. error = xfs_growfs_data(mp, &in);
  1234. return -error;
  1235. }
  1236. case XFS_IOC_FSGROWFSLOG: {
  1237. xfs_growfs_log_t in;
  1238. if (copy_from_user(&in, arg, sizeof(in)))
  1239. return -XFS_ERROR(EFAULT);
  1240. error = xfs_growfs_log(mp, &in);
  1241. return -error;
  1242. }
  1243. case XFS_IOC_FSGROWFSRT: {
  1244. xfs_growfs_rt_t in;
  1245. if (copy_from_user(&in, arg, sizeof(in)))
  1246. return -XFS_ERROR(EFAULT);
  1247. error = xfs_growfs_rt(mp, &in);
  1248. return -error;
  1249. }
  1250. case XFS_IOC_GOINGDOWN: {
  1251. __uint32_t in;
  1252. if (!capable(CAP_SYS_ADMIN))
  1253. return -EPERM;
  1254. if (get_user(in, (__uint32_t __user *)arg))
  1255. return -XFS_ERROR(EFAULT);
  1256. error = xfs_fs_goingdown(mp, in);
  1257. return -error;
  1258. }
  1259. case XFS_IOC_ERROR_INJECTION: {
  1260. xfs_error_injection_t in;
  1261. if (!capable(CAP_SYS_ADMIN))
  1262. return -EPERM;
  1263. if (copy_from_user(&in, arg, sizeof(in)))
  1264. return -XFS_ERROR(EFAULT);
  1265. error = xfs_errortag_add(in.errtag, mp);
  1266. return -error;
  1267. }
  1268. case XFS_IOC_ERROR_CLEARALL:
  1269. if (!capable(CAP_SYS_ADMIN))
  1270. return -EPERM;
  1271. error = xfs_errortag_clearall(mp, 1);
  1272. return -error;
  1273. default:
  1274. return -ENOTTY;
  1275. }
  1276. }