xfs_ioctl.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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_RDONLY(inode))
  463. return -EROFS;
  464. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  465. return EPERM;
  466. if (len > XATTR_SIZE_MAX)
  467. return EINVAL;
  468. kbuf = kmalloc(len, GFP_KERNEL);
  469. if (!kbuf)
  470. return ENOMEM;
  471. if (copy_from_user(kbuf, ubuf, len))
  472. goto out_kfree;
  473. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  474. out_kfree:
  475. kfree(kbuf);
  476. return error;
  477. }
  478. STATIC int
  479. xfs_attrmulti_attr_remove(
  480. struct inode *inode,
  481. char *name,
  482. __uint32_t flags)
  483. {
  484. if (IS_RDONLY(inode))
  485. return -EROFS;
  486. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  487. return EPERM;
  488. return xfs_attr_remove(XFS_I(inode), name, flags);
  489. }
  490. STATIC int
  491. xfs_attrmulti_by_handle(
  492. xfs_mount_t *mp,
  493. void __user *arg,
  494. struct inode *parinode)
  495. {
  496. int error;
  497. xfs_attr_multiop_t *ops;
  498. xfs_fsop_attrmulti_handlereq_t am_hreq;
  499. struct inode *inode;
  500. unsigned int i, size;
  501. char *attr_name;
  502. if (!capable(CAP_SYS_ADMIN))
  503. return -XFS_ERROR(EPERM);
  504. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  505. return -XFS_ERROR(EFAULT);
  506. error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
  507. if (error)
  508. goto out;
  509. error = E2BIG;
  510. size = am_hreq.opcount * sizeof(attr_multiop_t);
  511. if (!size || size > 16 * PAGE_SIZE)
  512. goto out_vn_rele;
  513. error = ENOMEM;
  514. ops = kmalloc(size, GFP_KERNEL);
  515. if (!ops)
  516. goto out_vn_rele;
  517. error = EFAULT;
  518. if (copy_from_user(ops, am_hreq.ops, size))
  519. goto out_kfree_ops;
  520. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  521. if (!attr_name)
  522. goto out_kfree_ops;
  523. error = 0;
  524. for (i = 0; i < am_hreq.opcount; i++) {
  525. ops[i].am_error = strncpy_from_user(attr_name,
  526. ops[i].am_attrname, MAXNAMELEN);
  527. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  528. error = -ERANGE;
  529. if (ops[i].am_error < 0)
  530. break;
  531. switch (ops[i].am_opcode) {
  532. case ATTR_OP_GET:
  533. ops[i].am_error = xfs_attrmulti_attr_get(inode,
  534. attr_name, ops[i].am_attrvalue,
  535. &ops[i].am_length, ops[i].am_flags);
  536. break;
  537. case ATTR_OP_SET:
  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. break;
  542. case ATTR_OP_REMOVE:
  543. ops[i].am_error = xfs_attrmulti_attr_remove(inode,
  544. attr_name, ops[i].am_flags);
  545. break;
  546. default:
  547. ops[i].am_error = EINVAL;
  548. }
  549. }
  550. if (copy_to_user(am_hreq.ops, ops, size))
  551. error = XFS_ERROR(EFAULT);
  552. kfree(attr_name);
  553. out_kfree_ops:
  554. kfree(ops);
  555. out_vn_rele:
  556. iput(inode);
  557. out:
  558. return -error;
  559. }
  560. /* prototypes for a few of the stack-hungry cases that have
  561. * their own functions. Functions are defined after their use
  562. * so gcc doesn't get fancy and inline them with -03 */
  563. STATIC int
  564. xfs_ioc_space(
  565. struct xfs_inode *ip,
  566. struct inode *inode,
  567. struct file *filp,
  568. int flags,
  569. unsigned int cmd,
  570. void __user *arg);
  571. STATIC int
  572. xfs_ioc_bulkstat(
  573. xfs_mount_t *mp,
  574. unsigned int cmd,
  575. void __user *arg);
  576. STATIC int
  577. xfs_ioc_fsgeometry_v1(
  578. xfs_mount_t *mp,
  579. void __user *arg);
  580. STATIC int
  581. xfs_ioc_fsgeometry(
  582. xfs_mount_t *mp,
  583. void __user *arg);
  584. STATIC int
  585. xfs_ioc_xattr(
  586. xfs_inode_t *ip,
  587. struct file *filp,
  588. unsigned int cmd,
  589. void __user *arg);
  590. STATIC int
  591. xfs_ioc_fsgetxattr(
  592. xfs_inode_t *ip,
  593. int attr,
  594. void __user *arg);
  595. STATIC int
  596. xfs_ioc_getbmap(
  597. struct xfs_inode *ip,
  598. int flags,
  599. unsigned int cmd,
  600. void __user *arg);
  601. STATIC int
  602. xfs_ioc_getbmapx(
  603. struct xfs_inode *ip,
  604. void __user *arg);
  605. int
  606. xfs_ioctl(
  607. xfs_inode_t *ip,
  608. struct file *filp,
  609. int ioflags,
  610. unsigned int cmd,
  611. void __user *arg)
  612. {
  613. struct inode *inode = filp->f_path.dentry->d_inode;
  614. xfs_mount_t *mp = ip->i_mount;
  615. int error;
  616. xfs_itrace_entry(XFS_I(inode));
  617. switch (cmd) {
  618. case XFS_IOC_ALLOCSP:
  619. case XFS_IOC_FREESP:
  620. case XFS_IOC_RESVSP:
  621. case XFS_IOC_UNRESVSP:
  622. case XFS_IOC_ALLOCSP64:
  623. case XFS_IOC_FREESP64:
  624. case XFS_IOC_RESVSP64:
  625. case XFS_IOC_UNRESVSP64:
  626. /*
  627. * Only allow the sys admin to reserve space unless
  628. * unwritten extents are enabled.
  629. */
  630. if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
  631. !capable(CAP_SYS_ADMIN))
  632. return -EPERM;
  633. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
  634. case XFS_IOC_DIOINFO: {
  635. struct dioattr da;
  636. xfs_buftarg_t *target =
  637. XFS_IS_REALTIME_INODE(ip) ?
  638. mp->m_rtdev_targp : mp->m_ddev_targp;
  639. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  640. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  641. if (copy_to_user(arg, &da, sizeof(da)))
  642. return -XFS_ERROR(EFAULT);
  643. return 0;
  644. }
  645. case XFS_IOC_FSBULKSTAT_SINGLE:
  646. case XFS_IOC_FSBULKSTAT:
  647. case XFS_IOC_FSINUMBERS:
  648. return xfs_ioc_bulkstat(mp, cmd, arg);
  649. case XFS_IOC_FSGEOMETRY_V1:
  650. return xfs_ioc_fsgeometry_v1(mp, arg);
  651. case XFS_IOC_FSGEOMETRY:
  652. return xfs_ioc_fsgeometry(mp, arg);
  653. case XFS_IOC_GETVERSION:
  654. return put_user(inode->i_generation, (int __user *)arg);
  655. case XFS_IOC_FSGETXATTR:
  656. return xfs_ioc_fsgetxattr(ip, 0, arg);
  657. case XFS_IOC_FSGETXATTRA:
  658. return xfs_ioc_fsgetxattr(ip, 1, arg);
  659. case XFS_IOC_GETXFLAGS:
  660. case XFS_IOC_SETXFLAGS:
  661. case XFS_IOC_FSSETXATTR:
  662. return xfs_ioc_xattr(ip, filp, cmd, arg);
  663. case XFS_IOC_FSSETDM: {
  664. struct fsdmidata dmi;
  665. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  666. return -XFS_ERROR(EFAULT);
  667. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  668. dmi.fsd_dmstate);
  669. return -error;
  670. }
  671. case XFS_IOC_GETBMAP:
  672. case XFS_IOC_GETBMAPA:
  673. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  674. case XFS_IOC_GETBMAPX:
  675. return xfs_ioc_getbmapx(ip, arg);
  676. case XFS_IOC_FD_TO_HANDLE:
  677. case XFS_IOC_PATH_TO_HANDLE:
  678. case XFS_IOC_PATH_TO_FSHANDLE:
  679. return xfs_find_handle(cmd, arg);
  680. case XFS_IOC_OPEN_BY_HANDLE:
  681. return xfs_open_by_handle(mp, arg, filp, inode);
  682. case XFS_IOC_FSSETDM_BY_HANDLE:
  683. return xfs_fssetdm_by_handle(mp, arg, inode);
  684. case XFS_IOC_READLINK_BY_HANDLE:
  685. return xfs_readlink_by_handle(mp, arg, inode);
  686. case XFS_IOC_ATTRLIST_BY_HANDLE:
  687. return xfs_attrlist_by_handle(mp, arg, inode);
  688. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  689. return xfs_attrmulti_by_handle(mp, arg, inode);
  690. case XFS_IOC_SWAPEXT: {
  691. error = xfs_swapext((struct xfs_swapext __user *)arg);
  692. return -error;
  693. }
  694. case XFS_IOC_FSCOUNTS: {
  695. xfs_fsop_counts_t out;
  696. error = xfs_fs_counts(mp, &out);
  697. if (error)
  698. return -error;
  699. if (copy_to_user(arg, &out, sizeof(out)))
  700. return -XFS_ERROR(EFAULT);
  701. return 0;
  702. }
  703. case XFS_IOC_SET_RESBLKS: {
  704. xfs_fsop_resblks_t inout;
  705. __uint64_t in;
  706. if (!capable(CAP_SYS_ADMIN))
  707. return -EPERM;
  708. if (copy_from_user(&inout, arg, sizeof(inout)))
  709. return -XFS_ERROR(EFAULT);
  710. /* input parameter is passed in resblks field of structure */
  711. in = inout.resblks;
  712. error = xfs_reserve_blocks(mp, &in, &inout);
  713. if (error)
  714. return -error;
  715. if (copy_to_user(arg, &inout, sizeof(inout)))
  716. return -XFS_ERROR(EFAULT);
  717. return 0;
  718. }
  719. case XFS_IOC_GET_RESBLKS: {
  720. xfs_fsop_resblks_t out;
  721. if (!capable(CAP_SYS_ADMIN))
  722. return -EPERM;
  723. error = xfs_reserve_blocks(mp, NULL, &out);
  724. if (error)
  725. return -error;
  726. if (copy_to_user(arg, &out, sizeof(out)))
  727. return -XFS_ERROR(EFAULT);
  728. return 0;
  729. }
  730. case XFS_IOC_FSGROWFSDATA: {
  731. xfs_growfs_data_t in;
  732. if (!capable(CAP_SYS_ADMIN))
  733. return -EPERM;
  734. if (copy_from_user(&in, arg, sizeof(in)))
  735. return -XFS_ERROR(EFAULT);
  736. error = xfs_growfs_data(mp, &in);
  737. return -error;
  738. }
  739. case XFS_IOC_FSGROWFSLOG: {
  740. xfs_growfs_log_t in;
  741. if (!capable(CAP_SYS_ADMIN))
  742. return -EPERM;
  743. if (copy_from_user(&in, arg, sizeof(in)))
  744. return -XFS_ERROR(EFAULT);
  745. error = xfs_growfs_log(mp, &in);
  746. return -error;
  747. }
  748. case XFS_IOC_FSGROWFSRT: {
  749. xfs_growfs_rt_t in;
  750. if (!capable(CAP_SYS_ADMIN))
  751. return -EPERM;
  752. if (copy_from_user(&in, arg, sizeof(in)))
  753. return -XFS_ERROR(EFAULT);
  754. error = xfs_growfs_rt(mp, &in);
  755. return -error;
  756. }
  757. case XFS_IOC_FREEZE:
  758. if (!capable(CAP_SYS_ADMIN))
  759. return -EPERM;
  760. if (inode->i_sb->s_frozen == SB_UNFROZEN)
  761. freeze_bdev(inode->i_sb->s_bdev);
  762. return 0;
  763. case XFS_IOC_THAW:
  764. if (!capable(CAP_SYS_ADMIN))
  765. return -EPERM;
  766. if (inode->i_sb->s_frozen != SB_UNFROZEN)
  767. thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
  768. return 0;
  769. case XFS_IOC_GOINGDOWN: {
  770. __uint32_t in;
  771. if (!capable(CAP_SYS_ADMIN))
  772. return -EPERM;
  773. if (get_user(in, (__uint32_t __user *)arg))
  774. return -XFS_ERROR(EFAULT);
  775. error = xfs_fs_goingdown(mp, in);
  776. return -error;
  777. }
  778. case XFS_IOC_ERROR_INJECTION: {
  779. xfs_error_injection_t in;
  780. if (!capable(CAP_SYS_ADMIN))
  781. return -EPERM;
  782. if (copy_from_user(&in, arg, sizeof(in)))
  783. return -XFS_ERROR(EFAULT);
  784. error = xfs_errortag_add(in.errtag, mp);
  785. return -error;
  786. }
  787. case XFS_IOC_ERROR_CLEARALL:
  788. if (!capable(CAP_SYS_ADMIN))
  789. return -EPERM;
  790. error = xfs_errortag_clearall(mp, 1);
  791. return -error;
  792. default:
  793. return -ENOTTY;
  794. }
  795. }
  796. STATIC int
  797. xfs_ioc_space(
  798. struct xfs_inode *ip,
  799. struct inode *inode,
  800. struct file *filp,
  801. int ioflags,
  802. unsigned int cmd,
  803. void __user *arg)
  804. {
  805. xfs_flock64_t bf;
  806. int attr_flags = 0;
  807. int error;
  808. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  809. return -XFS_ERROR(EPERM);
  810. if (!(filp->f_mode & FMODE_WRITE))
  811. return -XFS_ERROR(EBADF);
  812. if (!S_ISREG(inode->i_mode))
  813. return -XFS_ERROR(EINVAL);
  814. if (copy_from_user(&bf, arg, sizeof(bf)))
  815. return -XFS_ERROR(EFAULT);
  816. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  817. attr_flags |= ATTR_NONBLOCK;
  818. if (ioflags & IO_INVIS)
  819. attr_flags |= ATTR_DMI;
  820. error = xfs_change_file_space(ip, cmd, &bf, filp->f_pos,
  821. NULL, attr_flags);
  822. return -error;
  823. }
  824. STATIC int
  825. xfs_ioc_bulkstat(
  826. xfs_mount_t *mp,
  827. unsigned int cmd,
  828. void __user *arg)
  829. {
  830. xfs_fsop_bulkreq_t bulkreq;
  831. int count; /* # of records returned */
  832. xfs_ino_t inlast; /* last inode number */
  833. int done;
  834. int error;
  835. /* done = 1 if there are more stats to get and if bulkstat */
  836. /* should be called again (unused here, but used in dmapi) */
  837. if (!capable(CAP_SYS_ADMIN))
  838. return -EPERM;
  839. if (XFS_FORCED_SHUTDOWN(mp))
  840. return -XFS_ERROR(EIO);
  841. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  842. return -XFS_ERROR(EFAULT);
  843. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  844. return -XFS_ERROR(EFAULT);
  845. if ((count = bulkreq.icount) <= 0)
  846. return -XFS_ERROR(EINVAL);
  847. if (bulkreq.ubuffer == NULL)
  848. return -XFS_ERROR(EINVAL);
  849. if (cmd == XFS_IOC_FSINUMBERS)
  850. error = xfs_inumbers(mp, &inlast, &count,
  851. bulkreq.ubuffer, xfs_inumbers_fmt);
  852. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  853. error = xfs_bulkstat_single(mp, &inlast,
  854. bulkreq.ubuffer, &done);
  855. else /* XFS_IOC_FSBULKSTAT */
  856. error = xfs_bulkstat(mp, &inlast, &count,
  857. (bulkstat_one_pf)xfs_bulkstat_one, NULL,
  858. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  859. BULKSTAT_FG_QUICK, &done);
  860. if (error)
  861. return -error;
  862. if (bulkreq.ocount != NULL) {
  863. if (copy_to_user(bulkreq.lastip, &inlast,
  864. sizeof(xfs_ino_t)))
  865. return -XFS_ERROR(EFAULT);
  866. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  867. return -XFS_ERROR(EFAULT);
  868. }
  869. return 0;
  870. }
  871. STATIC int
  872. xfs_ioc_fsgeometry_v1(
  873. xfs_mount_t *mp,
  874. void __user *arg)
  875. {
  876. xfs_fsop_geom_v1_t fsgeo;
  877. int error;
  878. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  879. if (error)
  880. return -error;
  881. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  882. return -XFS_ERROR(EFAULT);
  883. return 0;
  884. }
  885. STATIC int
  886. xfs_ioc_fsgeometry(
  887. xfs_mount_t *mp,
  888. void __user *arg)
  889. {
  890. xfs_fsop_geom_t fsgeo;
  891. int error;
  892. error = xfs_fs_geometry(mp, &fsgeo, 4);
  893. if (error)
  894. return -error;
  895. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  896. return -XFS_ERROR(EFAULT);
  897. return 0;
  898. }
  899. /*
  900. * Linux extended inode flags interface.
  901. */
  902. STATIC unsigned int
  903. xfs_merge_ioc_xflags(
  904. unsigned int flags,
  905. unsigned int start)
  906. {
  907. unsigned int xflags = start;
  908. if (flags & FS_IMMUTABLE_FL)
  909. xflags |= XFS_XFLAG_IMMUTABLE;
  910. else
  911. xflags &= ~XFS_XFLAG_IMMUTABLE;
  912. if (flags & FS_APPEND_FL)
  913. xflags |= XFS_XFLAG_APPEND;
  914. else
  915. xflags &= ~XFS_XFLAG_APPEND;
  916. if (flags & FS_SYNC_FL)
  917. xflags |= XFS_XFLAG_SYNC;
  918. else
  919. xflags &= ~XFS_XFLAG_SYNC;
  920. if (flags & FS_NOATIME_FL)
  921. xflags |= XFS_XFLAG_NOATIME;
  922. else
  923. xflags &= ~XFS_XFLAG_NOATIME;
  924. if (flags & FS_NODUMP_FL)
  925. xflags |= XFS_XFLAG_NODUMP;
  926. else
  927. xflags &= ~XFS_XFLAG_NODUMP;
  928. return xflags;
  929. }
  930. STATIC unsigned int
  931. xfs_di2lxflags(
  932. __uint16_t di_flags)
  933. {
  934. unsigned int flags = 0;
  935. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  936. flags |= FS_IMMUTABLE_FL;
  937. if (di_flags & XFS_DIFLAG_APPEND)
  938. flags |= FS_APPEND_FL;
  939. if (di_flags & XFS_DIFLAG_SYNC)
  940. flags |= FS_SYNC_FL;
  941. if (di_flags & XFS_DIFLAG_NOATIME)
  942. flags |= FS_NOATIME_FL;
  943. if (di_flags & XFS_DIFLAG_NODUMP)
  944. flags |= FS_NODUMP_FL;
  945. return flags;
  946. }
  947. STATIC int
  948. xfs_ioc_fsgetxattr(
  949. xfs_inode_t *ip,
  950. int attr,
  951. void __user *arg)
  952. {
  953. struct fsxattr fa;
  954. xfs_ilock(ip, XFS_ILOCK_SHARED);
  955. fa.fsx_xflags = xfs_ip2xflags(ip);
  956. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  957. fa.fsx_projid = ip->i_d.di_projid;
  958. if (attr) {
  959. if (ip->i_afp) {
  960. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  961. fa.fsx_nextents = ip->i_afp->if_bytes /
  962. sizeof(xfs_bmbt_rec_t);
  963. else
  964. fa.fsx_nextents = ip->i_d.di_anextents;
  965. } else
  966. fa.fsx_nextents = 0;
  967. } else {
  968. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  969. fa.fsx_nextents = ip->i_df.if_bytes /
  970. sizeof(xfs_bmbt_rec_t);
  971. else
  972. fa.fsx_nextents = ip->i_d.di_nextents;
  973. }
  974. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  975. if (copy_to_user(arg, &fa, sizeof(fa)))
  976. return -EFAULT;
  977. return 0;
  978. }
  979. STATIC int
  980. xfs_ioc_fssetxattr(
  981. xfs_inode_t *ip,
  982. struct file *filp,
  983. void __user *arg)
  984. {
  985. struct fsxattr fa;
  986. struct bhv_vattr *vattr;
  987. int error;
  988. int attr_flags;
  989. if (copy_from_user(&fa, arg, sizeof(fa)))
  990. return -EFAULT;
  991. vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
  992. if (unlikely(!vattr))
  993. return -ENOMEM;
  994. attr_flags = 0;
  995. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  996. attr_flags |= ATTR_NONBLOCK;
  997. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
  998. vattr->va_xflags = fa.fsx_xflags;
  999. vattr->va_extsize = fa.fsx_extsize;
  1000. vattr->va_projid = fa.fsx_projid;
  1001. error = -xfs_setattr(ip, vattr, attr_flags, NULL);
  1002. if (!error)
  1003. vn_revalidate(XFS_ITOV(ip)); /* update flags */
  1004. kfree(vattr);
  1005. return 0;
  1006. }
  1007. STATIC int
  1008. xfs_ioc_getxflags(
  1009. xfs_inode_t *ip,
  1010. void __user *arg)
  1011. {
  1012. unsigned int flags;
  1013. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1014. if (copy_to_user(arg, &flags, sizeof(flags)))
  1015. return -EFAULT;
  1016. return 0;
  1017. }
  1018. STATIC int
  1019. xfs_ioc_setxflags(
  1020. xfs_inode_t *ip,
  1021. struct file *filp,
  1022. void __user *arg)
  1023. {
  1024. struct bhv_vattr *vattr;
  1025. unsigned int flags;
  1026. int attr_flags;
  1027. int error;
  1028. if (copy_from_user(&flags, arg, sizeof(flags)))
  1029. return -EFAULT;
  1030. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1031. FS_NOATIME_FL | FS_NODUMP_FL | \
  1032. FS_SYNC_FL))
  1033. return -EOPNOTSUPP;
  1034. vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
  1035. if (unlikely(!vattr))
  1036. return -ENOMEM;
  1037. attr_flags = 0;
  1038. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1039. attr_flags |= ATTR_NONBLOCK;
  1040. vattr->va_mask = XFS_AT_XFLAGS;
  1041. vattr->va_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1042. error = -xfs_setattr(ip, vattr, attr_flags, NULL);
  1043. if (likely(!error))
  1044. vn_revalidate(XFS_ITOV(ip)); /* update flags */
  1045. kfree(vattr);
  1046. return error;
  1047. }
  1048. STATIC int
  1049. xfs_ioc_getbmap(
  1050. struct xfs_inode *ip,
  1051. int ioflags,
  1052. unsigned int cmd,
  1053. void __user *arg)
  1054. {
  1055. struct getbmap bm;
  1056. int iflags;
  1057. int error;
  1058. if (copy_from_user(&bm, arg, sizeof(bm)))
  1059. return -XFS_ERROR(EFAULT);
  1060. if (bm.bmv_count < 2)
  1061. return -XFS_ERROR(EINVAL);
  1062. iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1063. if (ioflags & IO_INVIS)
  1064. iflags |= BMV_IF_NO_DMAPI_READ;
  1065. error = xfs_getbmap(ip, &bm, (struct getbmap __user *)arg+1, iflags);
  1066. if (error)
  1067. return -error;
  1068. if (copy_to_user(arg, &bm, sizeof(bm)))
  1069. return -XFS_ERROR(EFAULT);
  1070. return 0;
  1071. }
  1072. STATIC int
  1073. xfs_ioc_getbmapx(
  1074. struct xfs_inode *ip,
  1075. void __user *arg)
  1076. {
  1077. struct getbmapx bmx;
  1078. struct getbmap bm;
  1079. int iflags;
  1080. int error;
  1081. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1082. return -XFS_ERROR(EFAULT);
  1083. if (bmx.bmv_count < 2)
  1084. return -XFS_ERROR(EINVAL);
  1085. /*
  1086. * Map input getbmapx structure to a getbmap
  1087. * structure for xfs_getbmap.
  1088. */
  1089. GETBMAP_CONVERT(bmx, bm);
  1090. iflags = bmx.bmv_iflags;
  1091. if (iflags & (~BMV_IF_VALID))
  1092. return -XFS_ERROR(EINVAL);
  1093. iflags |= BMV_IF_EXTENDED;
  1094. error = xfs_getbmap(ip, &bm, (struct getbmapx __user *)arg+1, iflags);
  1095. if (error)
  1096. return -error;
  1097. GETBMAP_CONVERT(bm, bmx);
  1098. if (copy_to_user(arg, &bmx, sizeof(bmx)))
  1099. return -XFS_ERROR(EFAULT);
  1100. return 0;
  1101. }
  1102. int
  1103. xfs_ioctl(
  1104. xfs_inode_t *ip,
  1105. struct file *filp,
  1106. int ioflags,
  1107. unsigned int cmd,
  1108. void __user *arg)
  1109. {
  1110. struct inode *inode = filp->f_path.dentry->d_inode;
  1111. xfs_mount_t *mp = ip->i_mount;
  1112. int error;
  1113. xfs_itrace_entry(XFS_I(inode));
  1114. switch (cmd) {
  1115. case XFS_IOC_ALLOCSP:
  1116. case XFS_IOC_FREESP:
  1117. case XFS_IOC_RESVSP:
  1118. case XFS_IOC_UNRESVSP:
  1119. case XFS_IOC_ALLOCSP64:
  1120. case XFS_IOC_FREESP64:
  1121. case XFS_IOC_RESVSP64:
  1122. case XFS_IOC_UNRESVSP64:
  1123. /*
  1124. * Only allow the sys admin to reserve space unless
  1125. * unwritten extents are enabled.
  1126. */
  1127. if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
  1128. !capable(CAP_SYS_ADMIN))
  1129. return -EPERM;
  1130. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
  1131. case XFS_IOC_DIOINFO: {
  1132. struct dioattr da;
  1133. xfs_buftarg_t *target =
  1134. XFS_IS_REALTIME_INODE(ip) ?
  1135. mp->m_rtdev_targp : mp->m_ddev_targp;
  1136. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  1137. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1138. if (copy_to_user(arg, &da, sizeof(da)))
  1139. return -XFS_ERROR(EFAULT);
  1140. return 0;
  1141. }
  1142. case XFS_IOC_FSBULKSTAT_SINGLE:
  1143. case XFS_IOC_FSBULKSTAT:
  1144. case XFS_IOC_FSINUMBERS:
  1145. return xfs_ioc_bulkstat(mp, cmd, arg);
  1146. case XFS_IOC_FSGEOMETRY_V1:
  1147. return xfs_ioc_fsgeometry_v1(mp, arg);
  1148. case XFS_IOC_FSGEOMETRY:
  1149. return xfs_ioc_fsgeometry(mp, arg);
  1150. case XFS_IOC_GETVERSION:
  1151. return put_user(inode->i_generation, (int __user *)arg);
  1152. case XFS_IOC_FSGETXATTR:
  1153. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1154. case XFS_IOC_FSGETXATTRA:
  1155. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1156. case XFS_IOC_FSSETXATTR:
  1157. return xfs_ioc_fssetxattr(ip, filp, arg);
  1158. case XFS_IOC_GETXFLAGS:
  1159. return xfs_ioc_getxflags(ip, arg);
  1160. case XFS_IOC_SETXFLAGS:
  1161. return xfs_ioc_setxflags(ip, filp, arg);
  1162. case XFS_IOC_FSSETDM: {
  1163. struct fsdmidata dmi;
  1164. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1165. return -XFS_ERROR(EFAULT);
  1166. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1167. dmi.fsd_dmstate);
  1168. return -error;
  1169. }
  1170. case XFS_IOC_GETBMAP:
  1171. case XFS_IOC_GETBMAPA:
  1172. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1173. case XFS_IOC_GETBMAPX:
  1174. return xfs_ioc_getbmapx(ip, arg);
  1175. case XFS_IOC_FD_TO_HANDLE:
  1176. case XFS_IOC_PATH_TO_HANDLE:
  1177. case XFS_IOC_PATH_TO_FSHANDLE:
  1178. return xfs_find_handle(cmd, arg);
  1179. case XFS_IOC_OPEN_BY_HANDLE:
  1180. return xfs_open_by_handle(mp, arg, filp, inode);
  1181. case XFS_IOC_FSSETDM_BY_HANDLE:
  1182. return xfs_fssetdm_by_handle(mp, arg, inode);
  1183. case XFS_IOC_READLINK_BY_HANDLE:
  1184. return xfs_readlink_by_handle(mp, arg, inode);
  1185. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1186. return xfs_attrlist_by_handle(mp, arg, inode);
  1187. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1188. return xfs_attrmulti_by_handle(mp, arg, inode);
  1189. case XFS_IOC_SWAPEXT: {
  1190. error = xfs_swapext((struct xfs_swapext __user *)arg);
  1191. return -error;
  1192. }
  1193. case XFS_IOC_FSCOUNTS: {
  1194. xfs_fsop_counts_t out;
  1195. error = xfs_fs_counts(mp, &out);
  1196. if (error)
  1197. return -error;
  1198. if (copy_to_user(arg, &out, sizeof(out)))
  1199. return -XFS_ERROR(EFAULT);
  1200. return 0;
  1201. }
  1202. case XFS_IOC_SET_RESBLKS: {
  1203. xfs_fsop_resblks_t inout;
  1204. __uint64_t in;
  1205. if (!capable(CAP_SYS_ADMIN))
  1206. return -EPERM;
  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 (!capable(CAP_SYS_ADMIN))
  1232. return -EPERM;
  1233. if (copy_from_user(&in, arg, sizeof(in)))
  1234. return -XFS_ERROR(EFAULT);
  1235. error = xfs_growfs_data(mp, &in);
  1236. return -error;
  1237. }
  1238. case XFS_IOC_FSGROWFSLOG: {
  1239. xfs_growfs_log_t in;
  1240. if (!capable(CAP_SYS_ADMIN))
  1241. return -EPERM;
  1242. if (copy_from_user(&in, arg, sizeof(in)))
  1243. return -XFS_ERROR(EFAULT);
  1244. error = xfs_growfs_log(mp, &in);
  1245. return -error;
  1246. }
  1247. case XFS_IOC_FSGROWFSRT: {
  1248. xfs_growfs_rt_t in;
  1249. if (!capable(CAP_SYS_ADMIN))
  1250. return -EPERM;
  1251. if (copy_from_user(&in, arg, sizeof(in)))
  1252. return -XFS_ERROR(EFAULT);
  1253. error = xfs_growfs_rt(mp, &in);
  1254. return -error;
  1255. }
  1256. case XFS_IOC_FREEZE:
  1257. if (!capable(CAP_SYS_ADMIN))
  1258. return -EPERM;
  1259. if (inode->i_sb->s_frozen == SB_UNFROZEN)
  1260. freeze_bdev(inode->i_sb->s_bdev);
  1261. return 0;
  1262. case XFS_IOC_THAW:
  1263. if (!capable(CAP_SYS_ADMIN))
  1264. return -EPERM;
  1265. if (inode->i_sb->s_frozen != SB_UNFROZEN)
  1266. thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
  1267. return 0;
  1268. case XFS_IOC_GOINGDOWN: {
  1269. __uint32_t in;
  1270. if (!capable(CAP_SYS_ADMIN))
  1271. return -EPERM;
  1272. if (get_user(in, (__uint32_t __user *)arg))
  1273. return -XFS_ERROR(EFAULT);
  1274. error = xfs_fs_goingdown(mp, in);
  1275. return -error;
  1276. }
  1277. case XFS_IOC_ERROR_INJECTION: {
  1278. xfs_error_injection_t in;
  1279. if (!capable(CAP_SYS_ADMIN))
  1280. return -EPERM;
  1281. if (copy_from_user(&in, arg, sizeof(in)))
  1282. return -XFS_ERROR(EFAULT);
  1283. error = xfs_errortag_add(in.errtag, mp);
  1284. return -error;
  1285. }
  1286. case XFS_IOC_ERROR_CLEARALL:
  1287. if (!capable(CAP_SYS_ADMIN))
  1288. return -EPERM;
  1289. error = xfs_errortag_clearall(mp, 1);
  1290. return -error;
  1291. default:
  1292. return -ENOTTY;
  1293. }
  1294. }