xfs_ioctl.c 34 KB

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