xfs_ioctl.c 41 KB

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