xfs_ioctl.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  456. if (IS_ERR(dentry))
  457. return PTR_ERR(dentry);
  458. error = E2BIG;
  459. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  460. if (!size || size > 16 * PAGE_SIZE)
  461. goto out_dput;
  462. ops = memdup_user(am_hreq.ops, size);
  463. if (IS_ERR(ops)) {
  464. error = PTR_ERR(ops);
  465. goto out_dput;
  466. }
  467. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  468. if (!attr_name)
  469. goto out_kfree_ops;
  470. error = 0;
  471. for (i = 0; i < am_hreq.opcount; i++) {
  472. ops[i].am_error = strncpy_from_user((char *)attr_name,
  473. ops[i].am_attrname, MAXNAMELEN);
  474. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  475. error = -ERANGE;
  476. if (ops[i].am_error < 0)
  477. break;
  478. switch (ops[i].am_opcode) {
  479. case ATTR_OP_GET:
  480. ops[i].am_error = xfs_attrmulti_attr_get(
  481. dentry->d_inode, attr_name,
  482. ops[i].am_attrvalue, &ops[i].am_length,
  483. ops[i].am_flags);
  484. break;
  485. case ATTR_OP_SET:
  486. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  487. if (ops[i].am_error)
  488. break;
  489. ops[i].am_error = xfs_attrmulti_attr_set(
  490. dentry->d_inode, attr_name,
  491. ops[i].am_attrvalue, ops[i].am_length,
  492. ops[i].am_flags);
  493. mnt_drop_write(parfilp->f_path.mnt);
  494. break;
  495. case ATTR_OP_REMOVE:
  496. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  497. if (ops[i].am_error)
  498. break;
  499. ops[i].am_error = xfs_attrmulti_attr_remove(
  500. dentry->d_inode, attr_name,
  501. ops[i].am_flags);
  502. mnt_drop_write(parfilp->f_path.mnt);
  503. break;
  504. default:
  505. ops[i].am_error = EINVAL;
  506. }
  507. }
  508. if (copy_to_user(am_hreq.ops, ops, size))
  509. error = XFS_ERROR(EFAULT);
  510. kfree(attr_name);
  511. out_kfree_ops:
  512. kfree(ops);
  513. out_dput:
  514. dput(dentry);
  515. return -error;
  516. }
  517. int
  518. xfs_ioc_space(
  519. struct xfs_inode *ip,
  520. struct inode *inode,
  521. struct file *filp,
  522. int ioflags,
  523. unsigned int cmd,
  524. xfs_flock64_t *bf)
  525. {
  526. int attr_flags = 0;
  527. int error;
  528. /*
  529. * Only allow the sys admin to reserve space unless
  530. * unwritten extents are enabled.
  531. */
  532. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  533. !capable(CAP_SYS_ADMIN))
  534. return -XFS_ERROR(EPERM);
  535. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  536. return -XFS_ERROR(EPERM);
  537. if (!(filp->f_mode & FMODE_WRITE))
  538. return -XFS_ERROR(EBADF);
  539. if (!S_ISREG(inode->i_mode))
  540. return -XFS_ERROR(EINVAL);
  541. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  542. attr_flags |= XFS_ATTR_NONBLOCK;
  543. if (ioflags & IO_INVIS)
  544. attr_flags |= XFS_ATTR_DMI;
  545. error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
  546. return -error;
  547. }
  548. STATIC int
  549. xfs_ioc_bulkstat(
  550. xfs_mount_t *mp,
  551. unsigned int cmd,
  552. void __user *arg)
  553. {
  554. xfs_fsop_bulkreq_t bulkreq;
  555. int count; /* # of records returned */
  556. xfs_ino_t inlast; /* last inode number */
  557. int done;
  558. int error;
  559. /* done = 1 if there are more stats to get and if bulkstat */
  560. /* should be called again (unused here, but used in dmapi) */
  561. if (!capable(CAP_SYS_ADMIN))
  562. return -EPERM;
  563. if (XFS_FORCED_SHUTDOWN(mp))
  564. return -XFS_ERROR(EIO);
  565. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  566. return -XFS_ERROR(EFAULT);
  567. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  568. return -XFS_ERROR(EFAULT);
  569. if ((count = bulkreq.icount) <= 0)
  570. return -XFS_ERROR(EINVAL);
  571. if (bulkreq.ubuffer == NULL)
  572. return -XFS_ERROR(EINVAL);
  573. if (cmd == XFS_IOC_FSINUMBERS)
  574. error = xfs_inumbers(mp, &inlast, &count,
  575. bulkreq.ubuffer, xfs_inumbers_fmt);
  576. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  577. error = xfs_bulkstat_single(mp, &inlast,
  578. bulkreq.ubuffer, &done);
  579. else /* XFS_IOC_FSBULKSTAT */
  580. error = xfs_bulkstat(mp, &inlast, &count,
  581. (bulkstat_one_pf)xfs_bulkstat_one, NULL,
  582. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  583. BULKSTAT_FG_QUICK, &done);
  584. if (error)
  585. return -error;
  586. if (bulkreq.ocount != NULL) {
  587. if (copy_to_user(bulkreq.lastip, &inlast,
  588. sizeof(xfs_ino_t)))
  589. return -XFS_ERROR(EFAULT);
  590. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  591. return -XFS_ERROR(EFAULT);
  592. }
  593. return 0;
  594. }
  595. STATIC int
  596. xfs_ioc_fsgeometry_v1(
  597. xfs_mount_t *mp,
  598. void __user *arg)
  599. {
  600. xfs_fsop_geom_v1_t fsgeo;
  601. int error;
  602. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  603. if (error)
  604. return -error;
  605. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  606. return -XFS_ERROR(EFAULT);
  607. return 0;
  608. }
  609. STATIC int
  610. xfs_ioc_fsgeometry(
  611. xfs_mount_t *mp,
  612. void __user *arg)
  613. {
  614. xfs_fsop_geom_t fsgeo;
  615. int error;
  616. error = xfs_fs_geometry(mp, &fsgeo, 4);
  617. if (error)
  618. return -error;
  619. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  620. return -XFS_ERROR(EFAULT);
  621. return 0;
  622. }
  623. /*
  624. * Linux extended inode flags interface.
  625. */
  626. STATIC unsigned int
  627. xfs_merge_ioc_xflags(
  628. unsigned int flags,
  629. unsigned int start)
  630. {
  631. unsigned int xflags = start;
  632. if (flags & FS_IMMUTABLE_FL)
  633. xflags |= XFS_XFLAG_IMMUTABLE;
  634. else
  635. xflags &= ~XFS_XFLAG_IMMUTABLE;
  636. if (flags & FS_APPEND_FL)
  637. xflags |= XFS_XFLAG_APPEND;
  638. else
  639. xflags &= ~XFS_XFLAG_APPEND;
  640. if (flags & FS_SYNC_FL)
  641. xflags |= XFS_XFLAG_SYNC;
  642. else
  643. xflags &= ~XFS_XFLAG_SYNC;
  644. if (flags & FS_NOATIME_FL)
  645. xflags |= XFS_XFLAG_NOATIME;
  646. else
  647. xflags &= ~XFS_XFLAG_NOATIME;
  648. if (flags & FS_NODUMP_FL)
  649. xflags |= XFS_XFLAG_NODUMP;
  650. else
  651. xflags &= ~XFS_XFLAG_NODUMP;
  652. return xflags;
  653. }
  654. STATIC unsigned int
  655. xfs_di2lxflags(
  656. __uint16_t di_flags)
  657. {
  658. unsigned int flags = 0;
  659. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  660. flags |= FS_IMMUTABLE_FL;
  661. if (di_flags & XFS_DIFLAG_APPEND)
  662. flags |= FS_APPEND_FL;
  663. if (di_flags & XFS_DIFLAG_SYNC)
  664. flags |= FS_SYNC_FL;
  665. if (di_flags & XFS_DIFLAG_NOATIME)
  666. flags |= FS_NOATIME_FL;
  667. if (di_flags & XFS_DIFLAG_NODUMP)
  668. flags |= FS_NODUMP_FL;
  669. return flags;
  670. }
  671. STATIC int
  672. xfs_ioc_fsgetxattr(
  673. xfs_inode_t *ip,
  674. int attr,
  675. void __user *arg)
  676. {
  677. struct fsxattr fa;
  678. xfs_ilock(ip, XFS_ILOCK_SHARED);
  679. fa.fsx_xflags = xfs_ip2xflags(ip);
  680. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  681. fa.fsx_projid = ip->i_d.di_projid;
  682. if (attr) {
  683. if (ip->i_afp) {
  684. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  685. fa.fsx_nextents = ip->i_afp->if_bytes /
  686. sizeof(xfs_bmbt_rec_t);
  687. else
  688. fa.fsx_nextents = ip->i_d.di_anextents;
  689. } else
  690. fa.fsx_nextents = 0;
  691. } else {
  692. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  693. fa.fsx_nextents = ip->i_df.if_bytes /
  694. sizeof(xfs_bmbt_rec_t);
  695. else
  696. fa.fsx_nextents = ip->i_d.di_nextents;
  697. }
  698. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  699. if (copy_to_user(arg, &fa, sizeof(fa)))
  700. return -EFAULT;
  701. return 0;
  702. }
  703. STATIC void
  704. xfs_set_diflags(
  705. struct xfs_inode *ip,
  706. unsigned int xflags)
  707. {
  708. unsigned int di_flags;
  709. /* can't set PREALLOC this way, just preserve it */
  710. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  711. if (xflags & XFS_XFLAG_IMMUTABLE)
  712. di_flags |= XFS_DIFLAG_IMMUTABLE;
  713. if (xflags & XFS_XFLAG_APPEND)
  714. di_flags |= XFS_DIFLAG_APPEND;
  715. if (xflags & XFS_XFLAG_SYNC)
  716. di_flags |= XFS_DIFLAG_SYNC;
  717. if (xflags & XFS_XFLAG_NOATIME)
  718. di_flags |= XFS_DIFLAG_NOATIME;
  719. if (xflags & XFS_XFLAG_NODUMP)
  720. di_flags |= XFS_DIFLAG_NODUMP;
  721. if (xflags & XFS_XFLAG_PROJINHERIT)
  722. di_flags |= XFS_DIFLAG_PROJINHERIT;
  723. if (xflags & XFS_XFLAG_NODEFRAG)
  724. di_flags |= XFS_DIFLAG_NODEFRAG;
  725. if (xflags & XFS_XFLAG_FILESTREAM)
  726. di_flags |= XFS_DIFLAG_FILESTREAM;
  727. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  728. if (xflags & XFS_XFLAG_RTINHERIT)
  729. di_flags |= XFS_DIFLAG_RTINHERIT;
  730. if (xflags & XFS_XFLAG_NOSYMLINKS)
  731. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  732. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  733. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  734. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  735. if (xflags & XFS_XFLAG_REALTIME)
  736. di_flags |= XFS_DIFLAG_REALTIME;
  737. if (xflags & XFS_XFLAG_EXTSIZE)
  738. di_flags |= XFS_DIFLAG_EXTSIZE;
  739. }
  740. ip->i_d.di_flags = di_flags;
  741. }
  742. STATIC void
  743. xfs_diflags_to_linux(
  744. struct xfs_inode *ip)
  745. {
  746. struct inode *inode = VFS_I(ip);
  747. unsigned int xflags = xfs_ip2xflags(ip);
  748. if (xflags & XFS_XFLAG_IMMUTABLE)
  749. inode->i_flags |= S_IMMUTABLE;
  750. else
  751. inode->i_flags &= ~S_IMMUTABLE;
  752. if (xflags & XFS_XFLAG_APPEND)
  753. inode->i_flags |= S_APPEND;
  754. else
  755. inode->i_flags &= ~S_APPEND;
  756. if (xflags & XFS_XFLAG_SYNC)
  757. inode->i_flags |= S_SYNC;
  758. else
  759. inode->i_flags &= ~S_SYNC;
  760. if (xflags & XFS_XFLAG_NOATIME)
  761. inode->i_flags |= S_NOATIME;
  762. else
  763. inode->i_flags &= ~S_NOATIME;
  764. }
  765. #define FSX_PROJID 1
  766. #define FSX_EXTSIZE 2
  767. #define FSX_XFLAGS 4
  768. #define FSX_NONBLOCK 8
  769. STATIC int
  770. xfs_ioctl_setattr(
  771. xfs_inode_t *ip,
  772. struct fsxattr *fa,
  773. int mask)
  774. {
  775. struct xfs_mount *mp = ip->i_mount;
  776. struct xfs_trans *tp;
  777. unsigned int lock_flags = 0;
  778. struct xfs_dquot *udqp = NULL;
  779. struct xfs_dquot *gdqp = NULL;
  780. struct xfs_dquot *olddquot = NULL;
  781. int code;
  782. xfs_itrace_entry(ip);
  783. if (mp->m_flags & XFS_MOUNT_RDONLY)
  784. return XFS_ERROR(EROFS);
  785. if (XFS_FORCED_SHUTDOWN(mp))
  786. return XFS_ERROR(EIO);
  787. /*
  788. * If disk quotas is on, we make sure that the dquots do exist on disk,
  789. * before we start any other transactions. Trying to do this later
  790. * is messy. We don't care to take a readlock to look at the ids
  791. * in inode here, because we can't hold it across the trans_reserve.
  792. * If the IDs do change before we take the ilock, we're covered
  793. * because the i_*dquot fields will get updated anyway.
  794. */
  795. if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
  796. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  797. ip->i_d.di_gid, fa->fsx_projid,
  798. XFS_QMOPT_PQUOTA, &udqp, &gdqp);
  799. if (code)
  800. return code;
  801. }
  802. /*
  803. * For the other attributes, we acquire the inode lock and
  804. * first do an error checking pass.
  805. */
  806. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  807. code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  808. if (code)
  809. goto error_return;
  810. lock_flags = XFS_ILOCK_EXCL;
  811. xfs_ilock(ip, lock_flags);
  812. /*
  813. * CAP_FOWNER overrides the following restrictions:
  814. *
  815. * The user ID of the calling process must be equal
  816. * to the file owner ID, except in cases where the
  817. * CAP_FSETID capability is applicable.
  818. */
  819. if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
  820. code = XFS_ERROR(EPERM);
  821. goto error_return;
  822. }
  823. /*
  824. * Do a quota reservation only if projid is actually going to change.
  825. */
  826. if (mask & FSX_PROJID) {
  827. if (XFS_IS_QUOTA_RUNNING(mp) &&
  828. XFS_IS_PQUOTA_ON(mp) &&
  829. ip->i_d.di_projid != fa->fsx_projid) {
  830. ASSERT(tp);
  831. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  832. capable(CAP_FOWNER) ?
  833. XFS_QMOPT_FORCE_RES : 0);
  834. if (code) /* out of quota */
  835. goto error_return;
  836. }
  837. }
  838. if (mask & FSX_EXTSIZE) {
  839. /*
  840. * Can't change extent size if any extents are allocated.
  841. */
  842. if (ip->i_d.di_nextents &&
  843. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  844. fa->fsx_extsize)) {
  845. code = XFS_ERROR(EINVAL); /* EFBIG? */
  846. goto error_return;
  847. }
  848. /*
  849. * Extent size must be a multiple of the appropriate block
  850. * size, if set at all.
  851. */
  852. if (fa->fsx_extsize != 0) {
  853. xfs_extlen_t size;
  854. if (XFS_IS_REALTIME_INODE(ip) ||
  855. ((mask & FSX_XFLAGS) &&
  856. (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
  857. size = mp->m_sb.sb_rextsize <<
  858. mp->m_sb.sb_blocklog;
  859. } else {
  860. size = mp->m_sb.sb_blocksize;
  861. }
  862. if (fa->fsx_extsize % size) {
  863. code = XFS_ERROR(EINVAL);
  864. goto error_return;
  865. }
  866. }
  867. }
  868. if (mask & FSX_XFLAGS) {
  869. /*
  870. * Can't change realtime flag if any extents are allocated.
  871. */
  872. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  873. (XFS_IS_REALTIME_INODE(ip)) !=
  874. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  875. code = XFS_ERROR(EINVAL); /* EFBIG? */
  876. goto error_return;
  877. }
  878. /*
  879. * If realtime flag is set then must have realtime data.
  880. */
  881. if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  882. if ((mp->m_sb.sb_rblocks == 0) ||
  883. (mp->m_sb.sb_rextsize == 0) ||
  884. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  885. code = XFS_ERROR(EINVAL);
  886. goto error_return;
  887. }
  888. }
  889. /*
  890. * Can't modify an immutable/append-only file unless
  891. * we have appropriate permission.
  892. */
  893. if ((ip->i_d.di_flags &
  894. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  895. (fa->fsx_xflags &
  896. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  897. !capable(CAP_LINUX_IMMUTABLE)) {
  898. code = XFS_ERROR(EPERM);
  899. goto error_return;
  900. }
  901. }
  902. xfs_trans_ijoin(tp, ip, lock_flags);
  903. xfs_trans_ihold(tp, ip);
  904. /*
  905. * Change file ownership. Must be the owner or privileged.
  906. */
  907. if (mask & FSX_PROJID) {
  908. /*
  909. * CAP_FSETID overrides the following restrictions:
  910. *
  911. * The set-user-ID and set-group-ID bits of a file will be
  912. * cleared upon successful return from chown()
  913. */
  914. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  915. !capable(CAP_FSETID))
  916. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  917. /*
  918. * Change the ownerships and register quota modifications
  919. * in the transaction.
  920. */
  921. if (ip->i_d.di_projid != fa->fsx_projid) {
  922. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  923. olddquot = xfs_qm_vop_chown(tp, ip,
  924. &ip->i_gdquot, gdqp);
  925. }
  926. ip->i_d.di_projid = fa->fsx_projid;
  927. /*
  928. * We may have to rev the inode as well as
  929. * the superblock version number since projids didn't
  930. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  931. */
  932. if (ip->i_d.di_version == 1)
  933. xfs_bump_ino_vers2(tp, ip);
  934. }
  935. }
  936. if (mask & FSX_EXTSIZE)
  937. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  938. if (mask & FSX_XFLAGS) {
  939. xfs_set_diflags(ip, fa->fsx_xflags);
  940. xfs_diflags_to_linux(ip);
  941. }
  942. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  943. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  944. XFS_STATS_INC(xs_ig_attrchg);
  945. /*
  946. * If this is a synchronous mount, make sure that the
  947. * transaction goes to disk before returning to the user.
  948. * This is slightly sub-optimal in that truncates require
  949. * two sync transactions instead of one for wsync filesystems.
  950. * One for the truncate and one for the timestamps since we
  951. * don't want to change the timestamps unless we're sure the
  952. * truncate worked. Truncates are less than 1% of the laddis
  953. * mix so this probably isn't worth the trouble to optimize.
  954. */
  955. if (mp->m_flags & XFS_MOUNT_WSYNC)
  956. xfs_trans_set_sync(tp);
  957. code = xfs_trans_commit(tp, 0);
  958. xfs_iunlock(ip, lock_flags);
  959. /*
  960. * Release any dquot(s) the inode had kept before chown.
  961. */
  962. xfs_qm_dqrele(olddquot);
  963. xfs_qm_dqrele(udqp);
  964. xfs_qm_dqrele(gdqp);
  965. if (code)
  966. return code;
  967. if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
  968. XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
  969. NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
  970. (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
  971. }
  972. return 0;
  973. error_return:
  974. xfs_qm_dqrele(udqp);
  975. xfs_qm_dqrele(gdqp);
  976. xfs_trans_cancel(tp, 0);
  977. if (lock_flags)
  978. xfs_iunlock(ip, lock_flags);
  979. return code;
  980. }
  981. STATIC int
  982. xfs_ioc_fssetxattr(
  983. xfs_inode_t *ip,
  984. struct file *filp,
  985. void __user *arg)
  986. {
  987. struct fsxattr fa;
  988. unsigned int mask;
  989. if (copy_from_user(&fa, arg, sizeof(fa)))
  990. return -EFAULT;
  991. mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
  992. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  993. mask |= FSX_NONBLOCK;
  994. return -xfs_ioctl_setattr(ip, &fa, mask);
  995. }
  996. STATIC int
  997. xfs_ioc_getxflags(
  998. xfs_inode_t *ip,
  999. void __user *arg)
  1000. {
  1001. unsigned int flags;
  1002. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1003. if (copy_to_user(arg, &flags, sizeof(flags)))
  1004. return -EFAULT;
  1005. return 0;
  1006. }
  1007. STATIC int
  1008. xfs_ioc_setxflags(
  1009. xfs_inode_t *ip,
  1010. struct file *filp,
  1011. void __user *arg)
  1012. {
  1013. struct fsxattr fa;
  1014. unsigned int flags;
  1015. unsigned int mask;
  1016. if (copy_from_user(&flags, arg, sizeof(flags)))
  1017. return -EFAULT;
  1018. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1019. FS_NOATIME_FL | FS_NODUMP_FL | \
  1020. FS_SYNC_FL))
  1021. return -EOPNOTSUPP;
  1022. mask = FSX_XFLAGS;
  1023. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1024. mask |= FSX_NONBLOCK;
  1025. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1026. return -xfs_ioctl_setattr(ip, &fa, mask);
  1027. }
  1028. STATIC int
  1029. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1030. {
  1031. struct getbmap __user *base = *ap;
  1032. /* copy only getbmap portion (not getbmapx) */
  1033. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1034. return XFS_ERROR(EFAULT);
  1035. *ap += sizeof(struct getbmap);
  1036. return 0;
  1037. }
  1038. STATIC int
  1039. xfs_ioc_getbmap(
  1040. struct xfs_inode *ip,
  1041. int ioflags,
  1042. unsigned int cmd,
  1043. void __user *arg)
  1044. {
  1045. struct getbmapx bmx;
  1046. int error;
  1047. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1048. return -XFS_ERROR(EFAULT);
  1049. if (bmx.bmv_count < 2)
  1050. return -XFS_ERROR(EINVAL);
  1051. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1052. if (ioflags & IO_INVIS)
  1053. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1054. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1055. (struct getbmap *)arg+1);
  1056. if (error)
  1057. return -error;
  1058. /* copy back header - only size of getbmap */
  1059. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1060. return -XFS_ERROR(EFAULT);
  1061. return 0;
  1062. }
  1063. STATIC int
  1064. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1065. {
  1066. struct getbmapx __user *base = *ap;
  1067. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1068. return XFS_ERROR(EFAULT);
  1069. *ap += sizeof(struct getbmapx);
  1070. return 0;
  1071. }
  1072. STATIC int
  1073. xfs_ioc_getbmapx(
  1074. struct xfs_inode *ip,
  1075. void __user *arg)
  1076. {
  1077. struct getbmapx bmx;
  1078. int error;
  1079. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1080. return -XFS_ERROR(EFAULT);
  1081. if (bmx.bmv_count < 2)
  1082. return -XFS_ERROR(EINVAL);
  1083. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1084. return -XFS_ERROR(EINVAL);
  1085. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1086. (struct getbmapx *)arg+1);
  1087. if (error)
  1088. return -error;
  1089. /* copy back header */
  1090. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1091. return -XFS_ERROR(EFAULT);
  1092. return 0;
  1093. }
  1094. /*
  1095. * Note: some of the ioctl's return positive numbers as a
  1096. * byte count indicating success, such as readlink_by_handle.
  1097. * So we don't "sign flip" like most other routines. This means
  1098. * true errors need to be returned as a negative value.
  1099. */
  1100. long
  1101. xfs_file_ioctl(
  1102. struct file *filp,
  1103. unsigned int cmd,
  1104. unsigned long p)
  1105. {
  1106. struct inode *inode = filp->f_path.dentry->d_inode;
  1107. struct xfs_inode *ip = XFS_I(inode);
  1108. struct xfs_mount *mp = ip->i_mount;
  1109. void __user *arg = (void __user *)p;
  1110. int ioflags = 0;
  1111. int error;
  1112. if (filp->f_mode & FMODE_NOCMTIME)
  1113. ioflags |= IO_INVIS;
  1114. xfs_itrace_entry(ip);
  1115. switch (cmd) {
  1116. case XFS_IOC_ALLOCSP:
  1117. case XFS_IOC_FREESP:
  1118. case XFS_IOC_RESVSP:
  1119. case XFS_IOC_UNRESVSP:
  1120. case XFS_IOC_ALLOCSP64:
  1121. case XFS_IOC_FREESP64:
  1122. case XFS_IOC_RESVSP64:
  1123. case XFS_IOC_UNRESVSP64: {
  1124. xfs_flock64_t bf;
  1125. if (copy_from_user(&bf, arg, sizeof(bf)))
  1126. return -XFS_ERROR(EFAULT);
  1127. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1128. }
  1129. case XFS_IOC_DIOINFO: {
  1130. struct dioattr da;
  1131. xfs_buftarg_t *target =
  1132. XFS_IS_REALTIME_INODE(ip) ?
  1133. mp->m_rtdev_targp : mp->m_ddev_targp;
  1134. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  1135. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1136. if (copy_to_user(arg, &da, sizeof(da)))
  1137. return -XFS_ERROR(EFAULT);
  1138. return 0;
  1139. }
  1140. case XFS_IOC_FSBULKSTAT_SINGLE:
  1141. case XFS_IOC_FSBULKSTAT:
  1142. case XFS_IOC_FSINUMBERS:
  1143. return xfs_ioc_bulkstat(mp, cmd, arg);
  1144. case XFS_IOC_FSGEOMETRY_V1:
  1145. return xfs_ioc_fsgeometry_v1(mp, arg);
  1146. case XFS_IOC_FSGEOMETRY:
  1147. return xfs_ioc_fsgeometry(mp, arg);
  1148. case XFS_IOC_GETVERSION:
  1149. return put_user(inode->i_generation, (int __user *)arg);
  1150. case XFS_IOC_FSGETXATTR:
  1151. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1152. case XFS_IOC_FSGETXATTRA:
  1153. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1154. case XFS_IOC_FSSETXATTR:
  1155. return xfs_ioc_fssetxattr(ip, filp, arg);
  1156. case XFS_IOC_GETXFLAGS:
  1157. return xfs_ioc_getxflags(ip, arg);
  1158. case XFS_IOC_SETXFLAGS:
  1159. return xfs_ioc_setxflags(ip, filp, arg);
  1160. case XFS_IOC_FSSETDM: {
  1161. struct fsdmidata dmi;
  1162. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1163. return -XFS_ERROR(EFAULT);
  1164. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1165. dmi.fsd_dmstate);
  1166. return -error;
  1167. }
  1168. case XFS_IOC_GETBMAP:
  1169. case XFS_IOC_GETBMAPA:
  1170. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1171. case XFS_IOC_GETBMAPX:
  1172. return xfs_ioc_getbmapx(ip, arg);
  1173. case XFS_IOC_FD_TO_HANDLE:
  1174. case XFS_IOC_PATH_TO_HANDLE:
  1175. case XFS_IOC_PATH_TO_FSHANDLE: {
  1176. xfs_fsop_handlereq_t hreq;
  1177. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1178. return -XFS_ERROR(EFAULT);
  1179. return xfs_find_handle(cmd, &hreq);
  1180. }
  1181. case XFS_IOC_OPEN_BY_HANDLE: {
  1182. xfs_fsop_handlereq_t hreq;
  1183. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1184. return -XFS_ERROR(EFAULT);
  1185. return xfs_open_by_handle(filp, &hreq);
  1186. }
  1187. case XFS_IOC_FSSETDM_BY_HANDLE:
  1188. return xfs_fssetdm_by_handle(filp, arg);
  1189. case XFS_IOC_READLINK_BY_HANDLE: {
  1190. xfs_fsop_handlereq_t hreq;
  1191. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1192. return -XFS_ERROR(EFAULT);
  1193. return xfs_readlink_by_handle(filp, &hreq);
  1194. }
  1195. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1196. return xfs_attrlist_by_handle(filp, arg);
  1197. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1198. return xfs_attrmulti_by_handle(filp, arg);
  1199. case XFS_IOC_SWAPEXT: {
  1200. struct xfs_swapext sxp;
  1201. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1202. return -XFS_ERROR(EFAULT);
  1203. error = xfs_swapext(&sxp);
  1204. return -error;
  1205. }
  1206. case XFS_IOC_FSCOUNTS: {
  1207. xfs_fsop_counts_t out;
  1208. error = xfs_fs_counts(mp, &out);
  1209. if (error)
  1210. return -error;
  1211. if (copy_to_user(arg, &out, sizeof(out)))
  1212. return -XFS_ERROR(EFAULT);
  1213. return 0;
  1214. }
  1215. case XFS_IOC_SET_RESBLKS: {
  1216. xfs_fsop_resblks_t inout;
  1217. __uint64_t in;
  1218. if (!capable(CAP_SYS_ADMIN))
  1219. return -EPERM;
  1220. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1221. return -XFS_ERROR(EROFS);
  1222. if (copy_from_user(&inout, arg, sizeof(inout)))
  1223. return -XFS_ERROR(EFAULT);
  1224. /* input parameter is passed in resblks field of structure */
  1225. in = inout.resblks;
  1226. error = xfs_reserve_blocks(mp, &in, &inout);
  1227. if (error)
  1228. return -error;
  1229. if (copy_to_user(arg, &inout, sizeof(inout)))
  1230. return -XFS_ERROR(EFAULT);
  1231. return 0;
  1232. }
  1233. case XFS_IOC_GET_RESBLKS: {
  1234. xfs_fsop_resblks_t out;
  1235. if (!capable(CAP_SYS_ADMIN))
  1236. return -EPERM;
  1237. error = xfs_reserve_blocks(mp, NULL, &out);
  1238. if (error)
  1239. return -error;
  1240. if (copy_to_user(arg, &out, sizeof(out)))
  1241. return -XFS_ERROR(EFAULT);
  1242. return 0;
  1243. }
  1244. case XFS_IOC_FSGROWFSDATA: {
  1245. xfs_growfs_data_t in;
  1246. if (copy_from_user(&in, arg, sizeof(in)))
  1247. return -XFS_ERROR(EFAULT);
  1248. error = xfs_growfs_data(mp, &in);
  1249. return -error;
  1250. }
  1251. case XFS_IOC_FSGROWFSLOG: {
  1252. xfs_growfs_log_t in;
  1253. if (copy_from_user(&in, arg, sizeof(in)))
  1254. return -XFS_ERROR(EFAULT);
  1255. error = xfs_growfs_log(mp, &in);
  1256. return -error;
  1257. }
  1258. case XFS_IOC_FSGROWFSRT: {
  1259. xfs_growfs_rt_t in;
  1260. if (copy_from_user(&in, arg, sizeof(in)))
  1261. return -XFS_ERROR(EFAULT);
  1262. error = xfs_growfs_rt(mp, &in);
  1263. return -error;
  1264. }
  1265. case XFS_IOC_GOINGDOWN: {
  1266. __uint32_t in;
  1267. if (!capable(CAP_SYS_ADMIN))
  1268. return -EPERM;
  1269. if (get_user(in, (__uint32_t __user *)arg))
  1270. return -XFS_ERROR(EFAULT);
  1271. error = xfs_fs_goingdown(mp, in);
  1272. return -error;
  1273. }
  1274. case XFS_IOC_ERROR_INJECTION: {
  1275. xfs_error_injection_t in;
  1276. if (!capable(CAP_SYS_ADMIN))
  1277. return -EPERM;
  1278. if (copy_from_user(&in, arg, sizeof(in)))
  1279. return -XFS_ERROR(EFAULT);
  1280. error = xfs_errortag_add(in.errtag, mp);
  1281. return -error;
  1282. }
  1283. case XFS_IOC_ERROR_CLEARALL:
  1284. if (!capable(CAP_SYS_ADMIN))
  1285. return -EPERM;
  1286. error = xfs_errortag_clearall(mp, 1);
  1287. return -error;
  1288. default:
  1289. return -ENOTTY;
  1290. }
  1291. }