xfs_ioctl.c 39 KB

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