xfs_ioctl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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_dir2.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_rtalloc.h"
  40. #include "xfs_itable.h"
  41. #include "xfs_error.h"
  42. #include "xfs_rw.h"
  43. #include "xfs_acl.h"
  44. #include "xfs_attr.h"
  45. #include "xfs_bmap.h"
  46. #include "xfs_buf_item.h"
  47. #include "xfs_utils.h"
  48. #include "xfs_dfrag.h"
  49. #include "xfs_fsops.h"
  50. #include "xfs_vnodeops.h"
  51. #include <linux/capability.h>
  52. #include <linux/dcache.h>
  53. #include <linux/mount.h>
  54. #include <linux/namei.h>
  55. #include <linux/pagemap.h>
  56. /*
  57. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  58. * a file or fs handle.
  59. *
  60. * XFS_IOC_PATH_TO_FSHANDLE
  61. * returns fs handle for a mount point or path within that mount point
  62. * XFS_IOC_FD_TO_HANDLE
  63. * returns full handle for a FD opened in user space
  64. * XFS_IOC_PATH_TO_HANDLE
  65. * returns full handle for a path
  66. */
  67. STATIC int
  68. xfs_find_handle(
  69. unsigned int cmd,
  70. void __user *arg)
  71. {
  72. int hsize;
  73. xfs_handle_t handle;
  74. xfs_fsop_handlereq_t hreq;
  75. struct inode *inode;
  76. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  77. return -XFS_ERROR(EFAULT);
  78. memset((char *)&handle, 0, sizeof(handle));
  79. switch (cmd) {
  80. case XFS_IOC_PATH_TO_FSHANDLE:
  81. case XFS_IOC_PATH_TO_HANDLE: {
  82. struct nameidata nd;
  83. int error;
  84. error = user_path_walk_link((const char __user *)hreq.path, &nd);
  85. if (error)
  86. return error;
  87. ASSERT(nd.path.dentry);
  88. ASSERT(nd.path.dentry->d_inode);
  89. inode = igrab(nd.path.dentry->d_inode);
  90. path_put(&nd.path);
  91. break;
  92. }
  93. case XFS_IOC_FD_TO_HANDLE: {
  94. struct file *file;
  95. file = fget(hreq.fd);
  96. if (!file)
  97. return -EBADF;
  98. ASSERT(file->f_path.dentry);
  99. ASSERT(file->f_path.dentry->d_inode);
  100. inode = igrab(file->f_path.dentry->d_inode);
  101. fput(file);
  102. break;
  103. }
  104. default:
  105. ASSERT(0);
  106. return -XFS_ERROR(EINVAL);
  107. }
  108. if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
  109. /* we're not in XFS anymore, Toto */
  110. iput(inode);
  111. return -XFS_ERROR(EINVAL);
  112. }
  113. switch (inode->i_mode & S_IFMT) {
  114. case S_IFREG:
  115. case S_IFDIR:
  116. case S_IFLNK:
  117. break;
  118. default:
  119. iput(inode);
  120. return -XFS_ERROR(EBADF);
  121. }
  122. /* now we can grab the fsid */
  123. memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid,
  124. sizeof(xfs_fsid_t));
  125. hsize = sizeof(xfs_fsid_t);
  126. if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
  127. xfs_inode_t *ip = XFS_I(inode);
  128. int lock_mode;
  129. /* need to get access to the xfs_inode to read the generation */
  130. lock_mode = xfs_ilock_map_shared(ip);
  131. /* fill in fid section of handle from inode */
  132. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  133. sizeof(handle.ha_fid.fid_len);
  134. handle.ha_fid.fid_pad = 0;
  135. handle.ha_fid.fid_gen = ip->i_d.di_gen;
  136. handle.ha_fid.fid_ino = ip->i_ino;
  137. xfs_iunlock_map_shared(ip, lock_mode);
  138. hsize = XFS_HSIZE(handle);
  139. }
  140. /* now copy our handle into the user buffer & write out the size */
  141. if (copy_to_user(hreq.ohandle, &handle, hsize) ||
  142. copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
  143. iput(inode);
  144. return -XFS_ERROR(EFAULT);
  145. }
  146. iput(inode);
  147. return 0;
  148. }
  149. /*
  150. * Convert userspace handle data into inode.
  151. *
  152. * We use the fact that all the fsop_handlereq ioctl calls have a data
  153. * structure argument whose first component is always a xfs_fsop_handlereq_t,
  154. * so we can pass that sub structure into this handy, shared routine.
  155. *
  156. * If no error, caller must always iput the returned inode.
  157. */
  158. STATIC int
  159. xfs_vget_fsop_handlereq(
  160. xfs_mount_t *mp,
  161. struct inode *parinode, /* parent inode pointer */
  162. xfs_fsop_handlereq_t *hreq,
  163. struct inode **inode)
  164. {
  165. void __user *hanp;
  166. size_t hlen;
  167. xfs_fid_t *xfid;
  168. xfs_handle_t *handlep;
  169. xfs_handle_t handle;
  170. xfs_inode_t *ip;
  171. xfs_ino_t ino;
  172. __u32 igen;
  173. int error;
  174. /*
  175. * Only allow handle opens under a directory.
  176. */
  177. if (!S_ISDIR(parinode->i_mode))
  178. return XFS_ERROR(ENOTDIR);
  179. hanp = hreq->ihandle;
  180. hlen = hreq->ihandlen;
  181. handlep = &handle;
  182. if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
  183. return XFS_ERROR(EINVAL);
  184. if (copy_from_user(handlep, hanp, hlen))
  185. return XFS_ERROR(EFAULT);
  186. if (hlen < sizeof(*handlep))
  187. memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
  188. if (hlen > sizeof(handlep->ha_fsid)) {
  189. if (handlep->ha_fid.fid_len !=
  190. (hlen - sizeof(handlep->ha_fsid) -
  191. sizeof(handlep->ha_fid.fid_len)) ||
  192. handlep->ha_fid.fid_pad)
  193. return XFS_ERROR(EINVAL);
  194. }
  195. /*
  196. * Crack the handle, obtain the inode # & generation #
  197. */
  198. xfid = (struct xfs_fid *)&handlep->ha_fid;
  199. if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
  200. ino = xfid->fid_ino;
  201. igen = xfid->fid_gen;
  202. } else {
  203. return XFS_ERROR(EINVAL);
  204. }
  205. /*
  206. * Get the XFS inode, building a Linux inode to go with it.
  207. */
  208. error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
  209. if (error)
  210. return error;
  211. if (ip == NULL)
  212. return XFS_ERROR(EIO);
  213. if (ip->i_d.di_mode == 0 || ip->i_d.di_gen != igen) {
  214. xfs_iput_new(ip, XFS_ILOCK_SHARED);
  215. return XFS_ERROR(ENOENT);
  216. }
  217. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  218. *inode = XFS_ITOV(ip);
  219. return 0;
  220. }
  221. STATIC int
  222. xfs_open_by_handle(
  223. xfs_mount_t *mp,
  224. void __user *arg,
  225. struct file *parfilp,
  226. struct inode *parinode)
  227. {
  228. int error;
  229. int new_fd;
  230. int permflag;
  231. struct file *filp;
  232. struct inode *inode;
  233. struct dentry *dentry;
  234. xfs_fsop_handlereq_t hreq;
  235. if (!capable(CAP_SYS_ADMIN))
  236. return -XFS_ERROR(EPERM);
  237. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  238. return -XFS_ERROR(EFAULT);
  239. error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
  240. if (error)
  241. return -error;
  242. /* Restrict xfs_open_by_handle to directories & regular files. */
  243. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  244. iput(inode);
  245. return -XFS_ERROR(EINVAL);
  246. }
  247. #if BITS_PER_LONG != 32
  248. hreq.oflags |= O_LARGEFILE;
  249. #endif
  250. /* Put open permission in namei format. */
  251. permflag = hreq.oflags;
  252. if ((permflag+1) & O_ACCMODE)
  253. permflag++;
  254. if (permflag & O_TRUNC)
  255. permflag |= 2;
  256. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  257. (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
  258. iput(inode);
  259. return -XFS_ERROR(EPERM);
  260. }
  261. if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  262. iput(inode);
  263. return -XFS_ERROR(EACCES);
  264. }
  265. /* Can't write directories. */
  266. if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
  267. iput(inode);
  268. return -XFS_ERROR(EISDIR);
  269. }
  270. if ((new_fd = get_unused_fd()) < 0) {
  271. iput(inode);
  272. return new_fd;
  273. }
  274. dentry = d_alloc_anon(inode);
  275. if (dentry == NULL) {
  276. iput(inode);
  277. put_unused_fd(new_fd);
  278. return -XFS_ERROR(ENOMEM);
  279. }
  280. /* Ensure umount returns EBUSY on umounts while this file is open. */
  281. mntget(parfilp->f_path.mnt);
  282. /* Create file pointer. */
  283. filp = dentry_open(dentry, parfilp->f_path.mnt, hreq.oflags);
  284. if (IS_ERR(filp)) {
  285. put_unused_fd(new_fd);
  286. return -XFS_ERROR(-PTR_ERR(filp));
  287. }
  288. if (inode->i_mode & S_IFREG) {
  289. /* invisible operation should not change atime */
  290. filp->f_flags |= O_NOATIME;
  291. filp->f_op = &xfs_invis_file_operations;
  292. }
  293. fd_install(new_fd, filp);
  294. return new_fd;
  295. }
  296. /*
  297. * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
  298. * unused first argument.
  299. */
  300. STATIC int
  301. do_readlink(
  302. char __user *buffer,
  303. int buflen,
  304. const char *link)
  305. {
  306. int len;
  307. len = PTR_ERR(link);
  308. if (IS_ERR(link))
  309. goto out;
  310. len = strlen(link);
  311. if (len > (unsigned) buflen)
  312. len = buflen;
  313. if (copy_to_user(buffer, link, len))
  314. len = -EFAULT;
  315. out:
  316. return len;
  317. }
  318. STATIC int
  319. xfs_readlink_by_handle(
  320. xfs_mount_t *mp,
  321. void __user *arg,
  322. struct inode *parinode)
  323. {
  324. struct inode *inode;
  325. xfs_fsop_handlereq_t hreq;
  326. __u32 olen;
  327. void *link;
  328. int error;
  329. if (!capable(CAP_SYS_ADMIN))
  330. return -XFS_ERROR(EPERM);
  331. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  332. return -XFS_ERROR(EFAULT);
  333. error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
  334. if (error)
  335. return -error;
  336. /* Restrict this handle operation to symlinks only. */
  337. if (!S_ISLNK(inode->i_mode)) {
  338. error = -XFS_ERROR(EINVAL);
  339. goto out_iput;
  340. }
  341. if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
  342. error = -XFS_ERROR(EFAULT);
  343. goto out_iput;
  344. }
  345. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  346. if (!link)
  347. goto out_iput;
  348. error = -xfs_readlink(XFS_I(inode), link);
  349. if (error)
  350. goto out_kfree;
  351. error = do_readlink(hreq.ohandle, olen, link);
  352. if (error)
  353. goto out_kfree;
  354. out_kfree:
  355. kfree(link);
  356. out_iput:
  357. iput(inode);
  358. return error;
  359. }
  360. STATIC int
  361. xfs_fssetdm_by_handle(
  362. xfs_mount_t *mp,
  363. void __user *arg,
  364. struct inode *parinode)
  365. {
  366. int error;
  367. struct fsdmidata fsd;
  368. xfs_fsop_setdm_handlereq_t dmhreq;
  369. struct inode *inode;
  370. if (!capable(CAP_MKNOD))
  371. return -XFS_ERROR(EPERM);
  372. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  373. return -XFS_ERROR(EFAULT);
  374. error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
  375. if (error)
  376. return -error;
  377. if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
  378. error = -XFS_ERROR(EPERM);
  379. goto out;
  380. }
  381. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  382. error = -XFS_ERROR(EFAULT);
  383. goto out;
  384. }
  385. error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
  386. fsd.fsd_dmstate);
  387. out:
  388. iput(inode);
  389. return error;
  390. }
  391. STATIC int
  392. xfs_attrlist_by_handle(
  393. xfs_mount_t *mp,
  394. void __user *arg,
  395. struct inode *parinode)
  396. {
  397. int error;
  398. attrlist_cursor_kern_t *cursor;
  399. xfs_fsop_attrlist_handlereq_t al_hreq;
  400. struct inode *inode;
  401. char *kbuf;
  402. if (!capable(CAP_SYS_ADMIN))
  403. return -XFS_ERROR(EPERM);
  404. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  405. return -XFS_ERROR(EFAULT);
  406. if (al_hreq.buflen > XATTR_LIST_MAX)
  407. return -XFS_ERROR(EINVAL);
  408. error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
  409. if (error)
  410. goto out;
  411. kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
  412. if (!kbuf)
  413. goto out_vn_rele;
  414. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  415. error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
  416. al_hreq.flags, cursor);
  417. if (error)
  418. goto out_kfree;
  419. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  420. error = -EFAULT;
  421. out_kfree:
  422. kfree(kbuf);
  423. out_vn_rele:
  424. iput(inode);
  425. out:
  426. return -error;
  427. }
  428. STATIC int
  429. xfs_attrmulti_attr_get(
  430. struct inode *inode,
  431. char *name,
  432. char __user *ubuf,
  433. __uint32_t *len,
  434. __uint32_t flags)
  435. {
  436. char *kbuf;
  437. int error = EFAULT;
  438. if (*len > XATTR_SIZE_MAX)
  439. return EINVAL;
  440. kbuf = kmalloc(*len, GFP_KERNEL);
  441. if (!kbuf)
  442. return ENOMEM;
  443. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags, NULL);
  444. if (error)
  445. goto out_kfree;
  446. if (copy_to_user(ubuf, kbuf, *len))
  447. error = EFAULT;
  448. out_kfree:
  449. kfree(kbuf);
  450. return error;
  451. }
  452. STATIC int
  453. xfs_attrmulti_attr_set(
  454. struct inode *inode,
  455. char *name,
  456. const char __user *ubuf,
  457. __uint32_t len,
  458. __uint32_t flags)
  459. {
  460. char *kbuf;
  461. int error = EFAULT;
  462. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  463. return EPERM;
  464. if (len > XATTR_SIZE_MAX)
  465. return EINVAL;
  466. kbuf = kmalloc(len, GFP_KERNEL);
  467. if (!kbuf)
  468. return ENOMEM;
  469. if (copy_from_user(kbuf, ubuf, len))
  470. goto out_kfree;
  471. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  472. out_kfree:
  473. kfree(kbuf);
  474. return error;
  475. }
  476. STATIC int
  477. xfs_attrmulti_attr_remove(
  478. struct inode *inode,
  479. char *name,
  480. __uint32_t flags)
  481. {
  482. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  483. return EPERM;
  484. return xfs_attr_remove(XFS_I(inode), name, flags);
  485. }
  486. STATIC int
  487. xfs_attrmulti_by_handle(
  488. xfs_mount_t *mp,
  489. void __user *arg,
  490. struct file *parfilp,
  491. struct inode *parinode)
  492. {
  493. int error;
  494. xfs_attr_multiop_t *ops;
  495. xfs_fsop_attrmulti_handlereq_t am_hreq;
  496. struct inode *inode;
  497. unsigned int i, size;
  498. char *attr_name;
  499. if (!capable(CAP_SYS_ADMIN))
  500. return -XFS_ERROR(EPERM);
  501. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  502. return -XFS_ERROR(EFAULT);
  503. error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
  504. if (error)
  505. goto out;
  506. error = E2BIG;
  507. size = am_hreq.opcount * sizeof(attr_multiop_t);
  508. if (!size || size > 16 * PAGE_SIZE)
  509. goto out_vn_rele;
  510. error = ENOMEM;
  511. ops = kmalloc(size, GFP_KERNEL);
  512. if (!ops)
  513. goto out_vn_rele;
  514. error = EFAULT;
  515. if (copy_from_user(ops, am_hreq.ops, size))
  516. goto out_kfree_ops;
  517. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  518. if (!attr_name)
  519. goto out_kfree_ops;
  520. error = 0;
  521. for (i = 0; i < am_hreq.opcount; i++) {
  522. ops[i].am_error = strncpy_from_user(attr_name,
  523. ops[i].am_attrname, MAXNAMELEN);
  524. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  525. error = -ERANGE;
  526. if (ops[i].am_error < 0)
  527. break;
  528. switch (ops[i].am_opcode) {
  529. case ATTR_OP_GET:
  530. ops[i].am_error = xfs_attrmulti_attr_get(inode,
  531. attr_name, ops[i].am_attrvalue,
  532. &ops[i].am_length, ops[i].am_flags);
  533. break;
  534. case ATTR_OP_SET:
  535. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  536. if (ops[i].am_error)
  537. break;
  538. ops[i].am_error = xfs_attrmulti_attr_set(inode,
  539. attr_name, ops[i].am_attrvalue,
  540. ops[i].am_length, ops[i].am_flags);
  541. mnt_drop_write(parfilp->f_path.mnt);
  542. break;
  543. case ATTR_OP_REMOVE:
  544. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  545. if (ops[i].am_error)
  546. break;
  547. ops[i].am_error = xfs_attrmulti_attr_remove(inode,
  548. attr_name, ops[i].am_flags);
  549. mnt_drop_write(parfilp->f_path.mnt);
  550. break;
  551. default:
  552. ops[i].am_error = EINVAL;
  553. }
  554. }
  555. if (copy_to_user(am_hreq.ops, ops, size))
  556. error = XFS_ERROR(EFAULT);
  557. kfree(attr_name);
  558. out_kfree_ops:
  559. kfree(ops);
  560. out_vn_rele:
  561. iput(inode);
  562. out:
  563. return -error;
  564. }
  565. STATIC int
  566. xfs_ioc_space(
  567. struct xfs_inode *ip,
  568. struct inode *inode,
  569. struct file *filp,
  570. int ioflags,
  571. unsigned int cmd,
  572. void __user *arg)
  573. {
  574. xfs_flock64_t bf;
  575. int attr_flags = 0;
  576. int error;
  577. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  578. return -XFS_ERROR(EPERM);
  579. if (!(filp->f_mode & FMODE_WRITE))
  580. return -XFS_ERROR(EBADF);
  581. if (!S_ISREG(inode->i_mode))
  582. return -XFS_ERROR(EINVAL);
  583. if (copy_from_user(&bf, arg, sizeof(bf)))
  584. return -XFS_ERROR(EFAULT);
  585. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  586. attr_flags |= ATTR_NONBLOCK;
  587. if (ioflags & IO_INVIS)
  588. attr_flags |= ATTR_DMI;
  589. error = xfs_change_file_space(ip, cmd, &bf, filp->f_pos,
  590. NULL, attr_flags);
  591. return -error;
  592. }
  593. STATIC int
  594. xfs_ioc_bulkstat(
  595. xfs_mount_t *mp,
  596. unsigned int cmd,
  597. void __user *arg)
  598. {
  599. xfs_fsop_bulkreq_t bulkreq;
  600. int count; /* # of records returned */
  601. xfs_ino_t inlast; /* last inode number */
  602. int done;
  603. int error;
  604. /* done = 1 if there are more stats to get and if bulkstat */
  605. /* should be called again (unused here, but used in dmapi) */
  606. if (!capable(CAP_SYS_ADMIN))
  607. return -EPERM;
  608. if (XFS_FORCED_SHUTDOWN(mp))
  609. return -XFS_ERROR(EIO);
  610. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  611. return -XFS_ERROR(EFAULT);
  612. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  613. return -XFS_ERROR(EFAULT);
  614. if ((count = bulkreq.icount) <= 0)
  615. return -XFS_ERROR(EINVAL);
  616. if (bulkreq.ubuffer == NULL)
  617. return -XFS_ERROR(EINVAL);
  618. if (cmd == XFS_IOC_FSINUMBERS)
  619. error = xfs_inumbers(mp, &inlast, &count,
  620. bulkreq.ubuffer, xfs_inumbers_fmt);
  621. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  622. error = xfs_bulkstat_single(mp, &inlast,
  623. bulkreq.ubuffer, &done);
  624. else /* XFS_IOC_FSBULKSTAT */
  625. error = xfs_bulkstat(mp, &inlast, &count,
  626. (bulkstat_one_pf)xfs_bulkstat_one, NULL,
  627. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  628. BULKSTAT_FG_QUICK, &done);
  629. if (error)
  630. return -error;
  631. if (bulkreq.ocount != NULL) {
  632. if (copy_to_user(bulkreq.lastip, &inlast,
  633. sizeof(xfs_ino_t)))
  634. return -XFS_ERROR(EFAULT);
  635. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  636. return -XFS_ERROR(EFAULT);
  637. }
  638. return 0;
  639. }
  640. STATIC int
  641. xfs_ioc_fsgeometry_v1(
  642. xfs_mount_t *mp,
  643. void __user *arg)
  644. {
  645. xfs_fsop_geom_v1_t fsgeo;
  646. int error;
  647. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  648. if (error)
  649. return -error;
  650. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  651. return -XFS_ERROR(EFAULT);
  652. return 0;
  653. }
  654. STATIC int
  655. xfs_ioc_fsgeometry(
  656. xfs_mount_t *mp,
  657. void __user *arg)
  658. {
  659. xfs_fsop_geom_t fsgeo;
  660. int error;
  661. error = xfs_fs_geometry(mp, &fsgeo, 4);
  662. if (error)
  663. return -error;
  664. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  665. return -XFS_ERROR(EFAULT);
  666. return 0;
  667. }
  668. /*
  669. * Linux extended inode flags interface.
  670. */
  671. STATIC unsigned int
  672. xfs_merge_ioc_xflags(
  673. unsigned int flags,
  674. unsigned int start)
  675. {
  676. unsigned int xflags = start;
  677. if (flags & FS_IMMUTABLE_FL)
  678. xflags |= XFS_XFLAG_IMMUTABLE;
  679. else
  680. xflags &= ~XFS_XFLAG_IMMUTABLE;
  681. if (flags & FS_APPEND_FL)
  682. xflags |= XFS_XFLAG_APPEND;
  683. else
  684. xflags &= ~XFS_XFLAG_APPEND;
  685. if (flags & FS_SYNC_FL)
  686. xflags |= XFS_XFLAG_SYNC;
  687. else
  688. xflags &= ~XFS_XFLAG_SYNC;
  689. if (flags & FS_NOATIME_FL)
  690. xflags |= XFS_XFLAG_NOATIME;
  691. else
  692. xflags &= ~XFS_XFLAG_NOATIME;
  693. if (flags & FS_NODUMP_FL)
  694. xflags |= XFS_XFLAG_NODUMP;
  695. else
  696. xflags &= ~XFS_XFLAG_NODUMP;
  697. return xflags;
  698. }
  699. STATIC unsigned int
  700. xfs_di2lxflags(
  701. __uint16_t di_flags)
  702. {
  703. unsigned int flags = 0;
  704. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  705. flags |= FS_IMMUTABLE_FL;
  706. if (di_flags & XFS_DIFLAG_APPEND)
  707. flags |= FS_APPEND_FL;
  708. if (di_flags & XFS_DIFLAG_SYNC)
  709. flags |= FS_SYNC_FL;
  710. if (di_flags & XFS_DIFLAG_NOATIME)
  711. flags |= FS_NOATIME_FL;
  712. if (di_flags & XFS_DIFLAG_NODUMP)
  713. flags |= FS_NODUMP_FL;
  714. return flags;
  715. }
  716. STATIC int
  717. xfs_ioc_fsgetxattr(
  718. xfs_inode_t *ip,
  719. int attr,
  720. void __user *arg)
  721. {
  722. struct fsxattr fa;
  723. xfs_ilock(ip, XFS_ILOCK_SHARED);
  724. fa.fsx_xflags = xfs_ip2xflags(ip);
  725. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  726. fa.fsx_projid = ip->i_d.di_projid;
  727. if (attr) {
  728. if (ip->i_afp) {
  729. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  730. fa.fsx_nextents = ip->i_afp->if_bytes /
  731. sizeof(xfs_bmbt_rec_t);
  732. else
  733. fa.fsx_nextents = ip->i_d.di_anextents;
  734. } else
  735. fa.fsx_nextents = 0;
  736. } else {
  737. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  738. fa.fsx_nextents = ip->i_df.if_bytes /
  739. sizeof(xfs_bmbt_rec_t);
  740. else
  741. fa.fsx_nextents = ip->i_d.di_nextents;
  742. }
  743. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  744. if (copy_to_user(arg, &fa, sizeof(fa)))
  745. return -EFAULT;
  746. return 0;
  747. }
  748. STATIC int
  749. xfs_ioc_fssetxattr(
  750. xfs_inode_t *ip,
  751. struct file *filp,
  752. void __user *arg)
  753. {
  754. struct fsxattr fa;
  755. struct bhv_vattr *vattr;
  756. int error;
  757. int attr_flags;
  758. if (copy_from_user(&fa, arg, sizeof(fa)))
  759. return -EFAULT;
  760. vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
  761. if (unlikely(!vattr))
  762. return -ENOMEM;
  763. attr_flags = 0;
  764. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  765. attr_flags |= ATTR_NONBLOCK;
  766. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
  767. vattr->va_xflags = fa.fsx_xflags;
  768. vattr->va_extsize = fa.fsx_extsize;
  769. vattr->va_projid = fa.fsx_projid;
  770. error = -xfs_setattr(ip, vattr, attr_flags, NULL);
  771. if (!error)
  772. vn_revalidate(XFS_ITOV(ip)); /* update flags */
  773. kfree(vattr);
  774. return 0;
  775. }
  776. STATIC int
  777. xfs_ioc_getxflags(
  778. xfs_inode_t *ip,
  779. void __user *arg)
  780. {
  781. unsigned int flags;
  782. flags = xfs_di2lxflags(ip->i_d.di_flags);
  783. if (copy_to_user(arg, &flags, sizeof(flags)))
  784. return -EFAULT;
  785. return 0;
  786. }
  787. STATIC int
  788. xfs_ioc_setxflags(
  789. xfs_inode_t *ip,
  790. struct file *filp,
  791. void __user *arg)
  792. {
  793. struct bhv_vattr *vattr;
  794. unsigned int flags;
  795. int attr_flags;
  796. int error;
  797. if (copy_from_user(&flags, arg, sizeof(flags)))
  798. return -EFAULT;
  799. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  800. FS_NOATIME_FL | FS_NODUMP_FL | \
  801. FS_SYNC_FL))
  802. return -EOPNOTSUPP;
  803. vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
  804. if (unlikely(!vattr))
  805. return -ENOMEM;
  806. attr_flags = 0;
  807. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  808. attr_flags |= ATTR_NONBLOCK;
  809. vattr->va_mask = XFS_AT_XFLAGS;
  810. vattr->va_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  811. error = -xfs_setattr(ip, vattr, attr_flags, NULL);
  812. if (likely(!error))
  813. vn_revalidate(XFS_ITOV(ip)); /* update flags */
  814. kfree(vattr);
  815. return error;
  816. }
  817. STATIC int
  818. xfs_ioc_getbmap(
  819. struct xfs_inode *ip,
  820. int ioflags,
  821. unsigned int cmd,
  822. void __user *arg)
  823. {
  824. struct getbmap bm;
  825. int iflags;
  826. int error;
  827. if (copy_from_user(&bm, arg, sizeof(bm)))
  828. return -XFS_ERROR(EFAULT);
  829. if (bm.bmv_count < 2)
  830. return -XFS_ERROR(EINVAL);
  831. iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  832. if (ioflags & IO_INVIS)
  833. iflags |= BMV_IF_NO_DMAPI_READ;
  834. error = xfs_getbmap(ip, &bm, (struct getbmap __user *)arg+1, iflags);
  835. if (error)
  836. return -error;
  837. if (copy_to_user(arg, &bm, sizeof(bm)))
  838. return -XFS_ERROR(EFAULT);
  839. return 0;
  840. }
  841. STATIC int
  842. xfs_ioc_getbmapx(
  843. struct xfs_inode *ip,
  844. void __user *arg)
  845. {
  846. struct getbmapx bmx;
  847. struct getbmap bm;
  848. int iflags;
  849. int error;
  850. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  851. return -XFS_ERROR(EFAULT);
  852. if (bmx.bmv_count < 2)
  853. return -XFS_ERROR(EINVAL);
  854. /*
  855. * Map input getbmapx structure to a getbmap
  856. * structure for xfs_getbmap.
  857. */
  858. GETBMAP_CONVERT(bmx, bm);
  859. iflags = bmx.bmv_iflags;
  860. if (iflags & (~BMV_IF_VALID))
  861. return -XFS_ERROR(EINVAL);
  862. iflags |= BMV_IF_EXTENDED;
  863. error = xfs_getbmap(ip, &bm, (struct getbmapx __user *)arg+1, iflags);
  864. if (error)
  865. return -error;
  866. GETBMAP_CONVERT(bm, bmx);
  867. if (copy_to_user(arg, &bmx, sizeof(bmx)))
  868. return -XFS_ERROR(EFAULT);
  869. return 0;
  870. }
  871. int
  872. xfs_ioctl(
  873. xfs_inode_t *ip,
  874. struct file *filp,
  875. int ioflags,
  876. unsigned int cmd,
  877. void __user *arg)
  878. {
  879. struct inode *inode = filp->f_path.dentry->d_inode;
  880. xfs_mount_t *mp = ip->i_mount;
  881. int error;
  882. xfs_itrace_entry(XFS_I(inode));
  883. switch (cmd) {
  884. case XFS_IOC_ALLOCSP:
  885. case XFS_IOC_FREESP:
  886. case XFS_IOC_RESVSP:
  887. case XFS_IOC_UNRESVSP:
  888. case XFS_IOC_ALLOCSP64:
  889. case XFS_IOC_FREESP64:
  890. case XFS_IOC_RESVSP64:
  891. case XFS_IOC_UNRESVSP64:
  892. /*
  893. * Only allow the sys admin to reserve space unless
  894. * unwritten extents are enabled.
  895. */
  896. if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
  897. !capable(CAP_SYS_ADMIN))
  898. return -EPERM;
  899. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
  900. case XFS_IOC_DIOINFO: {
  901. struct dioattr da;
  902. xfs_buftarg_t *target =
  903. XFS_IS_REALTIME_INODE(ip) ?
  904. mp->m_rtdev_targp : mp->m_ddev_targp;
  905. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  906. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  907. if (copy_to_user(arg, &da, sizeof(da)))
  908. return -XFS_ERROR(EFAULT);
  909. return 0;
  910. }
  911. case XFS_IOC_FSBULKSTAT_SINGLE:
  912. case XFS_IOC_FSBULKSTAT:
  913. case XFS_IOC_FSINUMBERS:
  914. return xfs_ioc_bulkstat(mp, cmd, arg);
  915. case XFS_IOC_FSGEOMETRY_V1:
  916. return xfs_ioc_fsgeometry_v1(mp, arg);
  917. case XFS_IOC_FSGEOMETRY:
  918. return xfs_ioc_fsgeometry(mp, arg);
  919. case XFS_IOC_GETVERSION:
  920. return put_user(inode->i_generation, (int __user *)arg);
  921. case XFS_IOC_FSGETXATTR:
  922. return xfs_ioc_fsgetxattr(ip, 0, arg);
  923. case XFS_IOC_FSGETXATTRA:
  924. return xfs_ioc_fsgetxattr(ip, 1, arg);
  925. case XFS_IOC_FSSETXATTR:
  926. return xfs_ioc_fssetxattr(ip, filp, arg);
  927. case XFS_IOC_GETXFLAGS:
  928. return xfs_ioc_getxflags(ip, arg);
  929. case XFS_IOC_SETXFLAGS:
  930. return xfs_ioc_setxflags(ip, filp, arg);
  931. case XFS_IOC_FSSETDM: {
  932. struct fsdmidata dmi;
  933. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  934. return -XFS_ERROR(EFAULT);
  935. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  936. dmi.fsd_dmstate);
  937. return -error;
  938. }
  939. case XFS_IOC_GETBMAP:
  940. case XFS_IOC_GETBMAPA:
  941. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  942. case XFS_IOC_GETBMAPX:
  943. return xfs_ioc_getbmapx(ip, arg);
  944. case XFS_IOC_FD_TO_HANDLE:
  945. case XFS_IOC_PATH_TO_HANDLE:
  946. case XFS_IOC_PATH_TO_FSHANDLE:
  947. return xfs_find_handle(cmd, arg);
  948. case XFS_IOC_OPEN_BY_HANDLE:
  949. return xfs_open_by_handle(mp, arg, filp, inode);
  950. case XFS_IOC_FSSETDM_BY_HANDLE:
  951. return xfs_fssetdm_by_handle(mp, arg, inode);
  952. case XFS_IOC_READLINK_BY_HANDLE:
  953. return xfs_readlink_by_handle(mp, arg, inode);
  954. case XFS_IOC_ATTRLIST_BY_HANDLE:
  955. return xfs_attrlist_by_handle(mp, arg, inode);
  956. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  957. return xfs_attrmulti_by_handle(mp, arg, filp, inode);
  958. case XFS_IOC_SWAPEXT: {
  959. error = xfs_swapext((struct xfs_swapext __user *)arg);
  960. return -error;
  961. }
  962. case XFS_IOC_FSCOUNTS: {
  963. xfs_fsop_counts_t out;
  964. error = xfs_fs_counts(mp, &out);
  965. if (error)
  966. return -error;
  967. if (copy_to_user(arg, &out, sizeof(out)))
  968. return -XFS_ERROR(EFAULT);
  969. return 0;
  970. }
  971. case XFS_IOC_SET_RESBLKS: {
  972. xfs_fsop_resblks_t inout;
  973. __uint64_t in;
  974. if (!capable(CAP_SYS_ADMIN))
  975. return -EPERM;
  976. if (copy_from_user(&inout, arg, sizeof(inout)))
  977. return -XFS_ERROR(EFAULT);
  978. /* input parameter is passed in resblks field of structure */
  979. in = inout.resblks;
  980. error = xfs_reserve_blocks(mp, &in, &inout);
  981. if (error)
  982. return -error;
  983. if (copy_to_user(arg, &inout, sizeof(inout)))
  984. return -XFS_ERROR(EFAULT);
  985. return 0;
  986. }
  987. case XFS_IOC_GET_RESBLKS: {
  988. xfs_fsop_resblks_t out;
  989. if (!capable(CAP_SYS_ADMIN))
  990. return -EPERM;
  991. error = xfs_reserve_blocks(mp, NULL, &out);
  992. if (error)
  993. return -error;
  994. if (copy_to_user(arg, &out, sizeof(out)))
  995. return -XFS_ERROR(EFAULT);
  996. return 0;
  997. }
  998. case XFS_IOC_FSGROWFSDATA: {
  999. xfs_growfs_data_t in;
  1000. if (!capable(CAP_SYS_ADMIN))
  1001. return -EPERM;
  1002. if (copy_from_user(&in, arg, sizeof(in)))
  1003. return -XFS_ERROR(EFAULT);
  1004. error = xfs_growfs_data(mp, &in);
  1005. return -error;
  1006. }
  1007. case XFS_IOC_FSGROWFSLOG: {
  1008. xfs_growfs_log_t in;
  1009. if (!capable(CAP_SYS_ADMIN))
  1010. return -EPERM;
  1011. if (copy_from_user(&in, arg, sizeof(in)))
  1012. return -XFS_ERROR(EFAULT);
  1013. error = xfs_growfs_log(mp, &in);
  1014. return -error;
  1015. }
  1016. case XFS_IOC_FSGROWFSRT: {
  1017. xfs_growfs_rt_t in;
  1018. if (!capable(CAP_SYS_ADMIN))
  1019. return -EPERM;
  1020. if (copy_from_user(&in, arg, sizeof(in)))
  1021. return -XFS_ERROR(EFAULT);
  1022. error = xfs_growfs_rt(mp, &in);
  1023. return -error;
  1024. }
  1025. case XFS_IOC_FREEZE:
  1026. if (!capable(CAP_SYS_ADMIN))
  1027. return -EPERM;
  1028. if (inode->i_sb->s_frozen == SB_UNFROZEN)
  1029. freeze_bdev(inode->i_sb->s_bdev);
  1030. return 0;
  1031. case XFS_IOC_THAW:
  1032. if (!capable(CAP_SYS_ADMIN))
  1033. return -EPERM;
  1034. if (inode->i_sb->s_frozen != SB_UNFROZEN)
  1035. thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
  1036. return 0;
  1037. case XFS_IOC_GOINGDOWN: {
  1038. __uint32_t in;
  1039. if (!capable(CAP_SYS_ADMIN))
  1040. return -EPERM;
  1041. if (get_user(in, (__uint32_t __user *)arg))
  1042. return -XFS_ERROR(EFAULT);
  1043. error = xfs_fs_goingdown(mp, in);
  1044. return -error;
  1045. }
  1046. case XFS_IOC_ERROR_INJECTION: {
  1047. xfs_error_injection_t in;
  1048. if (!capable(CAP_SYS_ADMIN))
  1049. return -EPERM;
  1050. if (copy_from_user(&in, arg, sizeof(in)))
  1051. return -XFS_ERROR(EFAULT);
  1052. error = xfs_errortag_add(in.errtag, mp);
  1053. return -error;
  1054. }
  1055. case XFS_IOC_ERROR_CLEARALL:
  1056. if (!capable(CAP_SYS_ADMIN))
  1057. return -EPERM;
  1058. error = xfs_errortag_clearall(mp, 1);
  1059. return -error;
  1060. default:
  1061. return -ENOTTY;
  1062. }
  1063. }