xfs_ioctl.c 45 KB

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