xfs_ioctl.c 36 KB

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