xfs_ioctl.c 34 KB

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