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 "xfs_pnfs.h"
  43. #include "xfs_acl.h"
  44. #include <linux/capability.h>
  45. #include <linux/dcache.h>
  46. #include <linux/mount.h>
  47. #include <linux/namei.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/slab.h>
  50. #include <linux/exportfs.h>
  51. /*
  52. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  53. * a file or fs handle.
  54. *
  55. * XFS_IOC_PATH_TO_FSHANDLE
  56. * returns fs handle for a mount point or path within that mount point
  57. * XFS_IOC_FD_TO_HANDLE
  58. * returns full handle for a FD opened in user space
  59. * XFS_IOC_PATH_TO_HANDLE
  60. * returns full handle for a path
  61. */
  62. int
  63. xfs_find_handle(
  64. unsigned int cmd,
  65. xfs_fsop_handlereq_t *hreq)
  66. {
  67. int hsize;
  68. xfs_handle_t handle;
  69. struct inode *inode;
  70. struct fd f = {NULL};
  71. struct path path;
  72. int error;
  73. struct xfs_inode *ip;
  74. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  75. f = fdget(hreq->fd);
  76. if (!f.file)
  77. return -EBADF;
  78. inode = file_inode(f.file);
  79. } else {
  80. error = user_lpath((const char __user *)hreq->path, &path);
  81. if (error)
  82. return error;
  83. inode = d_inode(path.dentry);
  84. }
  85. ip = XFS_I(inode);
  86. /*
  87. * We can only generate handles for inodes residing on a XFS filesystem,
  88. * and only for regular files, directories or symbolic links.
  89. */
  90. error = -EINVAL;
  91. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  92. goto out_put;
  93. error = -EBADF;
  94. if (!S_ISREG(inode->i_mode) &&
  95. !S_ISDIR(inode->i_mode) &&
  96. !S_ISLNK(inode->i_mode))
  97. goto out_put;
  98. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  99. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  100. /*
  101. * This handle only contains an fsid, zero the rest.
  102. */
  103. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  104. hsize = sizeof(xfs_fsid_t);
  105. } else {
  106. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  107. sizeof(handle.ha_fid.fid_len);
  108. handle.ha_fid.fid_pad = 0;
  109. handle.ha_fid.fid_gen = ip->i_d.di_gen;
  110. handle.ha_fid.fid_ino = ip->i_ino;
  111. hsize = XFS_HSIZE(handle);
  112. }
  113. error = -EFAULT;
  114. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  115. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  116. goto out_put;
  117. error = 0;
  118. out_put:
  119. if (cmd == XFS_IOC_FD_TO_HANDLE)
  120. fdput(f);
  121. else
  122. path_put(&path);
  123. return error;
  124. }
  125. /*
  126. * No need to do permission checks on the various pathname components
  127. * as the handle operations are privileged.
  128. */
  129. STATIC int
  130. xfs_handle_acceptable(
  131. void *context,
  132. struct dentry *dentry)
  133. {
  134. return 1;
  135. }
  136. /*
  137. * Convert userspace handle data into a dentry.
  138. */
  139. struct dentry *
  140. xfs_handle_to_dentry(
  141. struct file *parfilp,
  142. void __user *uhandle,
  143. u32 hlen)
  144. {
  145. xfs_handle_t handle;
  146. struct xfs_fid64 fid;
  147. /*
  148. * Only allow handle opens under a directory.
  149. */
  150. if (!S_ISDIR(file_inode(parfilp)->i_mode))
  151. return ERR_PTR(-ENOTDIR);
  152. if (hlen != sizeof(xfs_handle_t))
  153. return ERR_PTR(-EINVAL);
  154. if (copy_from_user(&handle, uhandle, hlen))
  155. return ERR_PTR(-EFAULT);
  156. if (handle.ha_fid.fid_len !=
  157. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  158. return ERR_PTR(-EINVAL);
  159. memset(&fid, 0, sizeof(struct fid));
  160. fid.ino = handle.ha_fid.fid_ino;
  161. fid.gen = handle.ha_fid.fid_gen;
  162. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  163. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  164. xfs_handle_acceptable, NULL);
  165. }
  166. STATIC struct dentry *
  167. xfs_handlereq_to_dentry(
  168. struct file *parfilp,
  169. xfs_fsop_handlereq_t *hreq)
  170. {
  171. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  172. }
  173. int
  174. xfs_open_by_handle(
  175. struct file *parfilp,
  176. xfs_fsop_handlereq_t *hreq)
  177. {
  178. const struct cred *cred = current_cred();
  179. int error;
  180. int fd;
  181. int permflag;
  182. struct file *filp;
  183. struct inode *inode;
  184. struct dentry *dentry;
  185. fmode_t fmode;
  186. struct path path;
  187. if (!capable(CAP_SYS_ADMIN))
  188. return -EPERM;
  189. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  190. if (IS_ERR(dentry))
  191. return PTR_ERR(dentry);
  192. inode = d_inode(dentry);
  193. /* Restrict xfs_open_by_handle to directories & regular files. */
  194. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  195. error = -EPERM;
  196. goto out_dput;
  197. }
  198. #if BITS_PER_LONG != 32
  199. hreq->oflags |= O_LARGEFILE;
  200. #endif
  201. permflag = hreq->oflags;
  202. fmode = OPEN_FMODE(permflag);
  203. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  204. (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
  205. error = -EPERM;
  206. goto out_dput;
  207. }
  208. if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  209. error = -EACCES;
  210. goto out_dput;
  211. }
  212. /* Can't write directories. */
  213. if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
  214. error = -EISDIR;
  215. goto out_dput;
  216. }
  217. fd = get_unused_fd_flags(0);
  218. if (fd < 0) {
  219. error = fd;
  220. goto out_dput;
  221. }
  222. path.mnt = parfilp->f_path.mnt;
  223. path.dentry = dentry;
  224. filp = dentry_open(&path, hreq->oflags, cred);
  225. dput(dentry);
  226. if (IS_ERR(filp)) {
  227. put_unused_fd(fd);
  228. return PTR_ERR(filp);
  229. }
  230. if (S_ISREG(inode->i_mode)) {
  231. filp->f_flags |= O_NOATIME;
  232. filp->f_mode |= FMODE_NOCMTIME;
  233. }
  234. fd_install(fd, filp);
  235. return fd;
  236. out_dput:
  237. dput(dentry);
  238. return error;
  239. }
  240. int
  241. xfs_readlink_by_handle(
  242. struct file *parfilp,
  243. xfs_fsop_handlereq_t *hreq)
  244. {
  245. struct dentry *dentry;
  246. __u32 olen;
  247. void *link;
  248. int error;
  249. if (!capable(CAP_SYS_ADMIN))
  250. return -EPERM;
  251. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  252. if (IS_ERR(dentry))
  253. return PTR_ERR(dentry);
  254. /* Restrict this handle operation to symlinks only. */
  255. if (!d_is_symlink(dentry)) {
  256. error = -EINVAL;
  257. goto out_dput;
  258. }
  259. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  260. error = -EFAULT;
  261. goto out_dput;
  262. }
  263. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  264. if (!link) {
  265. error = -ENOMEM;
  266. goto out_dput;
  267. }
  268. error = xfs_readlink(XFS_I(d_inode(dentry)), link);
  269. if (error)
  270. goto out_kfree;
  271. error = readlink_copy(hreq->ohandle, olen, link);
  272. if (error)
  273. goto out_kfree;
  274. out_kfree:
  275. kfree(link);
  276. out_dput:
  277. dput(dentry);
  278. return error;
  279. }
  280. int
  281. xfs_set_dmattrs(
  282. xfs_inode_t *ip,
  283. u_int evmask,
  284. u_int16_t state)
  285. {
  286. xfs_mount_t *mp = ip->i_mount;
  287. xfs_trans_t *tp;
  288. int error;
  289. if (!capable(CAP_SYS_ADMIN))
  290. return -EPERM;
  291. if (XFS_FORCED_SHUTDOWN(mp))
  292. return -EIO;
  293. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  294. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  295. if (error) {
  296. xfs_trans_cancel(tp);
  297. return error;
  298. }
  299. xfs_ilock(ip, XFS_ILOCK_EXCL);
  300. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  301. ip->i_d.di_dmevmask = evmask;
  302. ip->i_d.di_dmstate = state;
  303. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  304. error = xfs_trans_commit(tp);
  305. return error;
  306. }
  307. STATIC int
  308. xfs_fssetdm_by_handle(
  309. struct file *parfilp,
  310. void __user *arg)
  311. {
  312. int error;
  313. struct fsdmidata fsd;
  314. xfs_fsop_setdm_handlereq_t dmhreq;
  315. struct dentry *dentry;
  316. if (!capable(CAP_MKNOD))
  317. return -EPERM;
  318. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  319. return -EFAULT;
  320. error = mnt_want_write_file(parfilp);
  321. if (error)
  322. return error;
  323. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  324. if (IS_ERR(dentry)) {
  325. mnt_drop_write_file(parfilp);
  326. return PTR_ERR(dentry);
  327. }
  328. if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
  329. error = -EPERM;
  330. goto out;
  331. }
  332. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  333. error = -EFAULT;
  334. goto out;
  335. }
  336. error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
  337. fsd.fsd_dmstate);
  338. out:
  339. mnt_drop_write_file(parfilp);
  340. dput(dentry);
  341. return error;
  342. }
  343. STATIC int
  344. xfs_attrlist_by_handle(
  345. struct file *parfilp,
  346. void __user *arg)
  347. {
  348. int error = -ENOMEM;
  349. attrlist_cursor_kern_t *cursor;
  350. struct xfs_fsop_attrlist_handlereq __user *p = arg;
  351. xfs_fsop_attrlist_handlereq_t al_hreq;
  352. struct dentry *dentry;
  353. char *kbuf;
  354. if (!capable(CAP_SYS_ADMIN))
  355. return -EPERM;
  356. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  357. return -EFAULT;
  358. if (al_hreq.buflen < sizeof(struct attrlist) ||
  359. al_hreq.buflen > XFS_XATTR_LIST_MAX)
  360. return -EINVAL;
  361. /*
  362. * Reject flags, only allow namespaces.
  363. */
  364. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  365. return -EINVAL;
  366. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  367. if (IS_ERR(dentry))
  368. return PTR_ERR(dentry);
  369. kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
  370. if (!kbuf)
  371. goto out_dput;
  372. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  373. error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
  374. al_hreq.flags, cursor);
  375. if (error)
  376. goto out_kfree;
  377. if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
  378. error = -EFAULT;
  379. goto out_kfree;
  380. }
  381. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  382. error = -EFAULT;
  383. out_kfree:
  384. kmem_free(kbuf);
  385. out_dput:
  386. dput(dentry);
  387. return error;
  388. }
  389. int
  390. xfs_attrmulti_attr_get(
  391. struct inode *inode,
  392. unsigned char *name,
  393. unsigned char __user *ubuf,
  394. __uint32_t *len,
  395. __uint32_t flags)
  396. {
  397. unsigned char *kbuf;
  398. int error = -EFAULT;
  399. if (*len > XFS_XATTR_SIZE_MAX)
  400. return -EINVAL;
  401. kbuf = kmem_zalloc_large(*len, KM_SLEEP);
  402. if (!kbuf)
  403. return -ENOMEM;
  404. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  405. if (error)
  406. goto out_kfree;
  407. if (copy_to_user(ubuf, kbuf, *len))
  408. error = -EFAULT;
  409. out_kfree:
  410. kmem_free(kbuf);
  411. return error;
  412. }
  413. int
  414. xfs_attrmulti_attr_set(
  415. struct inode *inode,
  416. unsigned char *name,
  417. const unsigned char __user *ubuf,
  418. __uint32_t len,
  419. __uint32_t flags)
  420. {
  421. unsigned char *kbuf;
  422. int error;
  423. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  424. return -EPERM;
  425. if (len > XFS_XATTR_SIZE_MAX)
  426. return -EINVAL;
  427. kbuf = memdup_user(ubuf, len);
  428. if (IS_ERR(kbuf))
  429. return PTR_ERR(kbuf);
  430. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  431. if (!error)
  432. xfs_forget_acl(inode, name, flags);
  433. kfree(kbuf);
  434. return error;
  435. }
  436. int
  437. xfs_attrmulti_attr_remove(
  438. struct inode *inode,
  439. unsigned char *name,
  440. __uint32_t flags)
  441. {
  442. int error;
  443. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  444. return -EPERM;
  445. error = xfs_attr_remove(XFS_I(inode), name, flags);
  446. if (!error)
  447. xfs_forget_acl(inode, name, flags);
  448. return error;
  449. }
  450. STATIC int
  451. xfs_attrmulti_by_handle(
  452. struct file *parfilp,
  453. void __user *arg)
  454. {
  455. int error;
  456. xfs_attr_multiop_t *ops;
  457. xfs_fsop_attrmulti_handlereq_t am_hreq;
  458. struct dentry *dentry;
  459. unsigned int i, size;
  460. unsigned char *attr_name;
  461. if (!capable(CAP_SYS_ADMIN))
  462. return -EPERM;
  463. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  464. return -EFAULT;
  465. /* overflow check */
  466. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  467. return -E2BIG;
  468. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  469. if (IS_ERR(dentry))
  470. return PTR_ERR(dentry);
  471. error = -E2BIG;
  472. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  473. if (!size || size > 16 * PAGE_SIZE)
  474. goto out_dput;
  475. ops = memdup_user(am_hreq.ops, size);
  476. if (IS_ERR(ops)) {
  477. error = PTR_ERR(ops);
  478. goto out_dput;
  479. }
  480. error = -ENOMEM;
  481. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  482. if (!attr_name)
  483. goto out_kfree_ops;
  484. error = 0;
  485. for (i = 0; i < am_hreq.opcount; i++) {
  486. ops[i].am_error = strncpy_from_user((char *)attr_name,
  487. ops[i].am_attrname, MAXNAMELEN);
  488. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  489. error = -ERANGE;
  490. if (ops[i].am_error < 0)
  491. break;
  492. switch (ops[i].am_opcode) {
  493. case ATTR_OP_GET:
  494. ops[i].am_error = xfs_attrmulti_attr_get(
  495. d_inode(dentry), attr_name,
  496. ops[i].am_attrvalue, &ops[i].am_length,
  497. ops[i].am_flags);
  498. break;
  499. case ATTR_OP_SET:
  500. ops[i].am_error = mnt_want_write_file(parfilp);
  501. if (ops[i].am_error)
  502. break;
  503. ops[i].am_error = xfs_attrmulti_attr_set(
  504. d_inode(dentry), attr_name,
  505. ops[i].am_attrvalue, ops[i].am_length,
  506. ops[i].am_flags);
  507. mnt_drop_write_file(parfilp);
  508. break;
  509. case ATTR_OP_REMOVE:
  510. ops[i].am_error = mnt_want_write_file(parfilp);
  511. if (ops[i].am_error)
  512. break;
  513. ops[i].am_error = xfs_attrmulti_attr_remove(
  514. d_inode(dentry), attr_name,
  515. ops[i].am_flags);
  516. mnt_drop_write_file(parfilp);
  517. break;
  518. default:
  519. ops[i].am_error = -EINVAL;
  520. }
  521. }
  522. if (copy_to_user(am_hreq.ops, ops, size))
  523. error = -EFAULT;
  524. kfree(attr_name);
  525. out_kfree_ops:
  526. kfree(ops);
  527. out_dput:
  528. dput(dentry);
  529. return error;
  530. }
  531. int
  532. xfs_ioc_space(
  533. struct xfs_inode *ip,
  534. struct inode *inode,
  535. struct file *filp,
  536. int ioflags,
  537. unsigned int cmd,
  538. xfs_flock64_t *bf)
  539. {
  540. struct iattr iattr;
  541. enum xfs_prealloc_flags flags = 0;
  542. uint iolock = XFS_IOLOCK_EXCL;
  543. int error;
  544. /*
  545. * Only allow the sys admin to reserve space unless
  546. * unwritten extents are enabled.
  547. */
  548. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  549. !capable(CAP_SYS_ADMIN))
  550. return -EPERM;
  551. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  552. return -EPERM;
  553. if (!(filp->f_mode & FMODE_WRITE))
  554. return -EBADF;
  555. if (!S_ISREG(inode->i_mode))
  556. return -EINVAL;
  557. if (filp->f_flags & O_DSYNC)
  558. flags |= XFS_PREALLOC_SYNC;
  559. if (ioflags & XFS_IO_INVIS)
  560. flags |= XFS_PREALLOC_INVISIBLE;
  561. error = mnt_want_write_file(filp);
  562. if (error)
  563. return error;
  564. xfs_ilock(ip, iolock);
  565. error = xfs_break_layouts(inode, &iolock, false);
  566. if (error)
  567. goto out_unlock;
  568. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  569. iolock |= XFS_MMAPLOCK_EXCL;
  570. switch (bf->l_whence) {
  571. case 0: /*SEEK_SET*/
  572. break;
  573. case 1: /*SEEK_CUR*/
  574. bf->l_start += filp->f_pos;
  575. break;
  576. case 2: /*SEEK_END*/
  577. bf->l_start += XFS_ISIZE(ip);
  578. break;
  579. default:
  580. error = -EINVAL;
  581. goto out_unlock;
  582. }
  583. /*
  584. * length of <= 0 for resv/unresv/zero is invalid. length for
  585. * alloc/free is ignored completely and we have no idea what userspace
  586. * might have set it to, so set it to zero to allow range
  587. * checks to pass.
  588. */
  589. switch (cmd) {
  590. case XFS_IOC_ZERO_RANGE:
  591. case XFS_IOC_RESVSP:
  592. case XFS_IOC_RESVSP64:
  593. case XFS_IOC_UNRESVSP:
  594. case XFS_IOC_UNRESVSP64:
  595. if (bf->l_len <= 0) {
  596. error = -EINVAL;
  597. goto out_unlock;
  598. }
  599. break;
  600. default:
  601. bf->l_len = 0;
  602. break;
  603. }
  604. if (bf->l_start < 0 ||
  605. bf->l_start > inode->i_sb->s_maxbytes ||
  606. bf->l_start + bf->l_len < 0 ||
  607. bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
  608. error = -EINVAL;
  609. goto out_unlock;
  610. }
  611. switch (cmd) {
  612. case XFS_IOC_ZERO_RANGE:
  613. flags |= XFS_PREALLOC_SET;
  614. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  615. break;
  616. case XFS_IOC_RESVSP:
  617. case XFS_IOC_RESVSP64:
  618. flags |= XFS_PREALLOC_SET;
  619. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  620. XFS_BMAPI_PREALLOC);
  621. break;
  622. case XFS_IOC_UNRESVSP:
  623. case XFS_IOC_UNRESVSP64:
  624. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  625. break;
  626. case XFS_IOC_ALLOCSP:
  627. case XFS_IOC_ALLOCSP64:
  628. case XFS_IOC_FREESP:
  629. case XFS_IOC_FREESP64:
  630. flags |= XFS_PREALLOC_CLEAR;
  631. if (bf->l_start > XFS_ISIZE(ip)) {
  632. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  633. bf->l_start - XFS_ISIZE(ip), 0);
  634. if (error)
  635. goto out_unlock;
  636. }
  637. iattr.ia_valid = ATTR_SIZE;
  638. iattr.ia_size = bf->l_start;
  639. error = xfs_setattr_size(ip, &iattr);
  640. break;
  641. default:
  642. ASSERT(0);
  643. error = -EINVAL;
  644. }
  645. if (error)
  646. goto out_unlock;
  647. error = xfs_update_prealloc_flags(ip, flags);
  648. out_unlock:
  649. xfs_iunlock(ip, iolock);
  650. mnt_drop_write_file(filp);
  651. return error;
  652. }
  653. STATIC int
  654. xfs_ioc_bulkstat(
  655. xfs_mount_t *mp,
  656. unsigned int cmd,
  657. void __user *arg)
  658. {
  659. xfs_fsop_bulkreq_t bulkreq;
  660. int count; /* # of records returned */
  661. xfs_ino_t inlast; /* last inode number */
  662. int done;
  663. int error;
  664. /* done = 1 if there are more stats to get and if bulkstat */
  665. /* should be called again (unused here, but used in dmapi) */
  666. if (!capable(CAP_SYS_ADMIN))
  667. return -EPERM;
  668. if (XFS_FORCED_SHUTDOWN(mp))
  669. return -EIO;
  670. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  671. return -EFAULT;
  672. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  673. return -EFAULT;
  674. if ((count = bulkreq.icount) <= 0)
  675. return -EINVAL;
  676. if (bulkreq.ubuffer == NULL)
  677. return -EINVAL;
  678. if (cmd == XFS_IOC_FSINUMBERS)
  679. error = xfs_inumbers(mp, &inlast, &count,
  680. bulkreq.ubuffer, xfs_inumbers_fmt);
  681. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  682. error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
  683. sizeof(xfs_bstat_t), NULL, &done);
  684. else /* XFS_IOC_FSBULKSTAT */
  685. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  686. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  687. &done);
  688. if (error)
  689. return error;
  690. if (bulkreq.ocount != NULL) {
  691. if (copy_to_user(bulkreq.lastip, &inlast,
  692. sizeof(xfs_ino_t)))
  693. return -EFAULT;
  694. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  695. return -EFAULT;
  696. }
  697. return 0;
  698. }
  699. STATIC int
  700. xfs_ioc_fsgeometry_v1(
  701. xfs_mount_t *mp,
  702. void __user *arg)
  703. {
  704. xfs_fsop_geom_t fsgeo;
  705. int error;
  706. error = xfs_fs_geometry(mp, &fsgeo, 3);
  707. if (error)
  708. return error;
  709. /*
  710. * Caller should have passed an argument of type
  711. * xfs_fsop_geom_v1_t. This is a proper subset of the
  712. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  713. */
  714. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  715. return -EFAULT;
  716. return 0;
  717. }
  718. STATIC int
  719. xfs_ioc_fsgeometry(
  720. xfs_mount_t *mp,
  721. void __user *arg)
  722. {
  723. xfs_fsop_geom_t fsgeo;
  724. int error;
  725. error = xfs_fs_geometry(mp, &fsgeo, 4);
  726. if (error)
  727. return error;
  728. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  729. return -EFAULT;
  730. return 0;
  731. }
  732. /*
  733. * Linux extended inode flags interface.
  734. */
  735. STATIC unsigned int
  736. xfs_merge_ioc_xflags(
  737. unsigned int flags,
  738. unsigned int start)
  739. {
  740. unsigned int xflags = start;
  741. if (flags & FS_IMMUTABLE_FL)
  742. xflags |= XFS_XFLAG_IMMUTABLE;
  743. else
  744. xflags &= ~XFS_XFLAG_IMMUTABLE;
  745. if (flags & FS_APPEND_FL)
  746. xflags |= XFS_XFLAG_APPEND;
  747. else
  748. xflags &= ~XFS_XFLAG_APPEND;
  749. if (flags & FS_SYNC_FL)
  750. xflags |= XFS_XFLAG_SYNC;
  751. else
  752. xflags &= ~XFS_XFLAG_SYNC;
  753. if (flags & FS_NOATIME_FL)
  754. xflags |= XFS_XFLAG_NOATIME;
  755. else
  756. xflags &= ~XFS_XFLAG_NOATIME;
  757. if (flags & FS_NODUMP_FL)
  758. xflags |= XFS_XFLAG_NODUMP;
  759. else
  760. xflags &= ~XFS_XFLAG_NODUMP;
  761. return xflags;
  762. }
  763. STATIC unsigned int
  764. xfs_di2lxflags(
  765. __uint16_t di_flags)
  766. {
  767. unsigned int flags = 0;
  768. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  769. flags |= FS_IMMUTABLE_FL;
  770. if (di_flags & XFS_DIFLAG_APPEND)
  771. flags |= FS_APPEND_FL;
  772. if (di_flags & XFS_DIFLAG_SYNC)
  773. flags |= FS_SYNC_FL;
  774. if (di_flags & XFS_DIFLAG_NOATIME)
  775. flags |= FS_NOATIME_FL;
  776. if (di_flags & XFS_DIFLAG_NODUMP)
  777. flags |= FS_NODUMP_FL;
  778. return flags;
  779. }
  780. STATIC int
  781. xfs_ioc_fsgetxattr(
  782. xfs_inode_t *ip,
  783. int attr,
  784. void __user *arg)
  785. {
  786. struct fsxattr fa;
  787. memset(&fa, 0, sizeof(struct fsxattr));
  788. xfs_ilock(ip, XFS_ILOCK_SHARED);
  789. fa.fsx_xflags = xfs_ip2xflags(ip);
  790. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  791. fa.fsx_projid = xfs_get_projid(ip);
  792. if (attr) {
  793. if (ip->i_afp) {
  794. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  795. fa.fsx_nextents = ip->i_afp->if_bytes /
  796. sizeof(xfs_bmbt_rec_t);
  797. else
  798. fa.fsx_nextents = ip->i_d.di_anextents;
  799. } else
  800. fa.fsx_nextents = 0;
  801. } else {
  802. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  803. fa.fsx_nextents = ip->i_df.if_bytes /
  804. sizeof(xfs_bmbt_rec_t);
  805. else
  806. fa.fsx_nextents = ip->i_d.di_nextents;
  807. }
  808. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  809. if (copy_to_user(arg, &fa, sizeof(fa)))
  810. return -EFAULT;
  811. return 0;
  812. }
  813. STATIC void
  814. xfs_set_diflags(
  815. struct xfs_inode *ip,
  816. unsigned int xflags)
  817. {
  818. unsigned int di_flags;
  819. /* can't set PREALLOC this way, just preserve it */
  820. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  821. if (xflags & XFS_XFLAG_IMMUTABLE)
  822. di_flags |= XFS_DIFLAG_IMMUTABLE;
  823. if (xflags & XFS_XFLAG_APPEND)
  824. di_flags |= XFS_DIFLAG_APPEND;
  825. if (xflags & XFS_XFLAG_SYNC)
  826. di_flags |= XFS_DIFLAG_SYNC;
  827. if (xflags & XFS_XFLAG_NOATIME)
  828. di_flags |= XFS_DIFLAG_NOATIME;
  829. if (xflags & XFS_XFLAG_NODUMP)
  830. di_flags |= XFS_DIFLAG_NODUMP;
  831. if (xflags & XFS_XFLAG_NODEFRAG)
  832. di_flags |= XFS_DIFLAG_NODEFRAG;
  833. if (xflags & XFS_XFLAG_FILESTREAM)
  834. di_flags |= XFS_DIFLAG_FILESTREAM;
  835. if (S_ISDIR(ip->i_d.di_mode)) {
  836. if (xflags & XFS_XFLAG_RTINHERIT)
  837. di_flags |= XFS_DIFLAG_RTINHERIT;
  838. if (xflags & XFS_XFLAG_NOSYMLINKS)
  839. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  840. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  841. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  842. if (xflags & XFS_XFLAG_PROJINHERIT)
  843. di_flags |= XFS_DIFLAG_PROJINHERIT;
  844. } else if (S_ISREG(ip->i_d.di_mode)) {
  845. if (xflags & XFS_XFLAG_REALTIME)
  846. di_flags |= XFS_DIFLAG_REALTIME;
  847. if (xflags & XFS_XFLAG_EXTSIZE)
  848. di_flags |= XFS_DIFLAG_EXTSIZE;
  849. }
  850. ip->i_d.di_flags = di_flags;
  851. }
  852. STATIC void
  853. xfs_diflags_to_linux(
  854. struct xfs_inode *ip)
  855. {
  856. struct inode *inode = VFS_I(ip);
  857. unsigned int xflags = xfs_ip2xflags(ip);
  858. if (xflags & XFS_XFLAG_IMMUTABLE)
  859. inode->i_flags |= S_IMMUTABLE;
  860. else
  861. inode->i_flags &= ~S_IMMUTABLE;
  862. if (xflags & XFS_XFLAG_APPEND)
  863. inode->i_flags |= S_APPEND;
  864. else
  865. inode->i_flags &= ~S_APPEND;
  866. if (xflags & XFS_XFLAG_SYNC)
  867. inode->i_flags |= S_SYNC;
  868. else
  869. inode->i_flags &= ~S_SYNC;
  870. if (xflags & XFS_XFLAG_NOATIME)
  871. inode->i_flags |= S_NOATIME;
  872. else
  873. inode->i_flags &= ~S_NOATIME;
  874. }
  875. static int
  876. xfs_ioctl_setattr_xflags(
  877. struct xfs_trans *tp,
  878. struct xfs_inode *ip,
  879. struct fsxattr *fa)
  880. {
  881. struct xfs_mount *mp = ip->i_mount;
  882. /* Can't change realtime flag if any extents are allocated. */
  883. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  884. XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & XFS_XFLAG_REALTIME))
  885. return -EINVAL;
  886. /* If realtime flag is set then must have realtime device */
  887. if (fa->fsx_xflags & XFS_XFLAG_REALTIME) {
  888. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  889. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
  890. return -EINVAL;
  891. }
  892. /*
  893. * Can't modify an immutable/append-only file unless
  894. * we have appropriate permission.
  895. */
  896. if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
  897. (fa->fsx_xflags & (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  898. !capable(CAP_LINUX_IMMUTABLE))
  899. return -EPERM;
  900. xfs_set_diflags(ip, fa->fsx_xflags);
  901. xfs_diflags_to_linux(ip);
  902. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  903. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  904. XFS_STATS_INC(mp, xs_ig_attrchg);
  905. return 0;
  906. }
  907. /*
  908. * Set up the transaction structure for the setattr operation, checking that we
  909. * have permission to do so. On success, return a clean transaction and the
  910. * inode locked exclusively ready for further operation specific checks. On
  911. * failure, return an error without modifying or locking the inode.
  912. */
  913. static struct xfs_trans *
  914. xfs_ioctl_setattr_get_trans(
  915. struct xfs_inode *ip)
  916. {
  917. struct xfs_mount *mp = ip->i_mount;
  918. struct xfs_trans *tp;
  919. int error;
  920. if (mp->m_flags & XFS_MOUNT_RDONLY)
  921. return ERR_PTR(-EROFS);
  922. if (XFS_FORCED_SHUTDOWN(mp))
  923. return ERR_PTR(-EIO);
  924. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  925. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  926. if (error)
  927. goto out_cancel;
  928. xfs_ilock(ip, XFS_ILOCK_EXCL);
  929. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  930. /*
  931. * CAP_FOWNER overrides the following restrictions:
  932. *
  933. * The user ID of the calling process must be equal to the file owner
  934. * ID, except in cases where the CAP_FSETID capability is applicable.
  935. */
  936. if (!inode_owner_or_capable(VFS_I(ip))) {
  937. error = -EPERM;
  938. goto out_cancel;
  939. }
  940. if (mp->m_flags & XFS_MOUNT_WSYNC)
  941. xfs_trans_set_sync(tp);
  942. return tp;
  943. out_cancel:
  944. xfs_trans_cancel(tp);
  945. return ERR_PTR(error);
  946. }
  947. /*
  948. * extent size hint validation is somewhat cumbersome. Rules are:
  949. *
  950. * 1. extent size hint is only valid for directories and regular files
  951. * 2. XFS_XFLAG_EXTSIZE is only valid for regular files
  952. * 3. XFS_XFLAG_EXTSZINHERIT is only valid for directories.
  953. * 4. can only be changed on regular files if no extents are allocated
  954. * 5. can be changed on directories at any time
  955. * 6. extsize hint of 0 turns off hints, clears inode flags.
  956. * 7. Extent size must be a multiple of the appropriate block size.
  957. * 8. for non-realtime files, the extent size hint must be limited
  958. * to half the AG size to avoid alignment extending the extent beyond the
  959. * limits of the AG.
  960. */
  961. static int
  962. xfs_ioctl_setattr_check_extsize(
  963. struct xfs_inode *ip,
  964. struct fsxattr *fa)
  965. {
  966. struct xfs_mount *mp = ip->i_mount;
  967. if ((fa->fsx_xflags & XFS_XFLAG_EXTSIZE) && !S_ISREG(ip->i_d.di_mode))
  968. return -EINVAL;
  969. if ((fa->fsx_xflags & XFS_XFLAG_EXTSZINHERIT) &&
  970. !S_ISDIR(ip->i_d.di_mode))
  971. return -EINVAL;
  972. if (S_ISREG(ip->i_d.di_mode) && ip->i_d.di_nextents &&
  973. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
  974. return -EINVAL;
  975. if (fa->fsx_extsize != 0) {
  976. xfs_extlen_t size;
  977. xfs_fsblock_t extsize_fsb;
  978. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  979. if (extsize_fsb > MAXEXTLEN)
  980. return -EINVAL;
  981. if (XFS_IS_REALTIME_INODE(ip) ||
  982. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  983. size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
  984. } else {
  985. size = mp->m_sb.sb_blocksize;
  986. if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
  987. return -EINVAL;
  988. }
  989. if (fa->fsx_extsize % size)
  990. return -EINVAL;
  991. } else
  992. fa->fsx_xflags &= ~(XFS_XFLAG_EXTSIZE | XFS_XFLAG_EXTSZINHERIT);
  993. return 0;
  994. }
  995. static int
  996. xfs_ioctl_setattr_check_projid(
  997. struct xfs_inode *ip,
  998. struct fsxattr *fa)
  999. {
  1000. /* Disallow 32bit project ids if projid32bit feature is not enabled. */
  1001. if (fa->fsx_projid > (__uint16_t)-1 &&
  1002. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  1003. return -EINVAL;
  1004. /*
  1005. * Project Quota ID state is only allowed to change from within the init
  1006. * namespace. Enforce that restriction only if we are trying to change
  1007. * the quota ID state. Everything else is allowed in user namespaces.
  1008. */
  1009. if (current_user_ns() == &init_user_ns)
  1010. return 0;
  1011. if (xfs_get_projid(ip) != fa->fsx_projid)
  1012. return -EINVAL;
  1013. if ((fa->fsx_xflags & XFS_XFLAG_PROJINHERIT) !=
  1014. (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  1015. return -EINVAL;
  1016. return 0;
  1017. }
  1018. STATIC int
  1019. xfs_ioctl_setattr(
  1020. xfs_inode_t *ip,
  1021. struct fsxattr *fa)
  1022. {
  1023. struct xfs_mount *mp = ip->i_mount;
  1024. struct xfs_trans *tp;
  1025. struct xfs_dquot *udqp = NULL;
  1026. struct xfs_dquot *pdqp = NULL;
  1027. struct xfs_dquot *olddquot = NULL;
  1028. int code;
  1029. trace_xfs_ioctl_setattr(ip);
  1030. code = xfs_ioctl_setattr_check_projid(ip, fa);
  1031. if (code)
  1032. return code;
  1033. /*
  1034. * If disk quotas is on, we make sure that the dquots do exist on disk,
  1035. * before we start any other transactions. Trying to do this later
  1036. * is messy. We don't care to take a readlock to look at the ids
  1037. * in inode here, because we can't hold it across the trans_reserve.
  1038. * If the IDs do change before we take the ilock, we're covered
  1039. * because the i_*dquot fields will get updated anyway.
  1040. */
  1041. if (XFS_IS_QUOTA_ON(mp)) {
  1042. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  1043. ip->i_d.di_gid, fa->fsx_projid,
  1044. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  1045. if (code)
  1046. return code;
  1047. }
  1048. tp = xfs_ioctl_setattr_get_trans(ip);
  1049. if (IS_ERR(tp)) {
  1050. code = PTR_ERR(tp);
  1051. goto error_free_dquots;
  1052. }
  1053. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
  1054. xfs_get_projid(ip) != fa->fsx_projid) {
  1055. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
  1056. capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
  1057. if (code) /* out of quota */
  1058. goto error_trans_cancel;
  1059. }
  1060. code = xfs_ioctl_setattr_check_extsize(ip, fa);
  1061. if (code)
  1062. goto error_trans_cancel;
  1063. code = xfs_ioctl_setattr_xflags(tp, ip, fa);
  1064. if (code)
  1065. goto error_trans_cancel;
  1066. /*
  1067. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  1068. * overrides the following restrictions:
  1069. *
  1070. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  1071. * successful return from chown()
  1072. */
  1073. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  1074. !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
  1075. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  1076. /* Change the ownerships and register project quota modifications */
  1077. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1078. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1079. olddquot = xfs_qm_vop_chown(tp, ip,
  1080. &ip->i_pdquot, pdqp);
  1081. }
  1082. ASSERT(ip->i_d.di_version > 1);
  1083. xfs_set_projid(ip, fa->fsx_projid);
  1084. }
  1085. /*
  1086. * Only set the extent size hint if we've already determined that the
  1087. * extent size hint should be set on the inode. If no extent size flags
  1088. * are set on the inode then unconditionally clear the extent size hint.
  1089. */
  1090. if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  1091. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1092. else
  1093. ip->i_d.di_extsize = 0;
  1094. code = xfs_trans_commit(tp);
  1095. /*
  1096. * Release any dquot(s) the inode had kept before chown.
  1097. */
  1098. xfs_qm_dqrele(olddquot);
  1099. xfs_qm_dqrele(udqp);
  1100. xfs_qm_dqrele(pdqp);
  1101. return code;
  1102. error_trans_cancel:
  1103. xfs_trans_cancel(tp);
  1104. error_free_dquots:
  1105. xfs_qm_dqrele(udqp);
  1106. xfs_qm_dqrele(pdqp);
  1107. return code;
  1108. }
  1109. STATIC int
  1110. xfs_ioc_fssetxattr(
  1111. xfs_inode_t *ip,
  1112. struct file *filp,
  1113. void __user *arg)
  1114. {
  1115. struct fsxattr fa;
  1116. int error;
  1117. if (copy_from_user(&fa, arg, sizeof(fa)))
  1118. return -EFAULT;
  1119. error = mnt_want_write_file(filp);
  1120. if (error)
  1121. return error;
  1122. error = xfs_ioctl_setattr(ip, &fa);
  1123. mnt_drop_write_file(filp);
  1124. return error;
  1125. }
  1126. STATIC int
  1127. xfs_ioc_getxflags(
  1128. xfs_inode_t *ip,
  1129. void __user *arg)
  1130. {
  1131. unsigned int flags;
  1132. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1133. if (copy_to_user(arg, &flags, sizeof(flags)))
  1134. return -EFAULT;
  1135. return 0;
  1136. }
  1137. STATIC int
  1138. xfs_ioc_setxflags(
  1139. struct xfs_inode *ip,
  1140. struct file *filp,
  1141. void __user *arg)
  1142. {
  1143. struct xfs_trans *tp;
  1144. struct fsxattr fa;
  1145. unsigned int flags;
  1146. int error;
  1147. if (copy_from_user(&flags, arg, sizeof(flags)))
  1148. return -EFAULT;
  1149. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1150. FS_NOATIME_FL | FS_NODUMP_FL | \
  1151. FS_SYNC_FL))
  1152. return -EOPNOTSUPP;
  1153. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1154. error = mnt_want_write_file(filp);
  1155. if (error)
  1156. return error;
  1157. tp = xfs_ioctl_setattr_get_trans(ip);
  1158. if (IS_ERR(tp)) {
  1159. error = PTR_ERR(tp);
  1160. goto out_drop_write;
  1161. }
  1162. error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
  1163. if (error) {
  1164. xfs_trans_cancel(tp);
  1165. goto out_drop_write;
  1166. }
  1167. error = xfs_trans_commit(tp);
  1168. out_drop_write:
  1169. mnt_drop_write_file(filp);
  1170. return error;
  1171. }
  1172. STATIC int
  1173. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1174. {
  1175. struct getbmap __user *base = (struct getbmap __user *)*ap;
  1176. /* copy only getbmap portion (not getbmapx) */
  1177. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1178. return -EFAULT;
  1179. *ap += sizeof(struct getbmap);
  1180. return 0;
  1181. }
  1182. STATIC int
  1183. xfs_ioc_getbmap(
  1184. struct xfs_inode *ip,
  1185. int ioflags,
  1186. unsigned int cmd,
  1187. void __user *arg)
  1188. {
  1189. struct getbmapx bmx = { 0 };
  1190. int error;
  1191. /* struct getbmap is a strict subset of struct getbmapx. */
  1192. if (copy_from_user(&bmx, arg, offsetof(struct getbmapx, bmv_iflags)))
  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. }