xfs_ioctl.c 37 KB

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