xfs_ioctl.c 40 KB

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