xfs_ioctl.c 39 KB

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