xfs_ioctl.c 35 KB

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