xfs_ioctl.c 40 KB

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