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