xfs_ioctl.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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();
  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 = kzalloc(al_hreq.buflen, GFP_KERNEL);
  368. if (!kbuf)
  369. goto out_dput;
  370. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  371. error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
  372. al_hreq.flags, cursor);
  373. if (error)
  374. goto out_kfree;
  375. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  376. error = -EFAULT;
  377. out_kfree:
  378. kfree(kbuf);
  379. out_dput:
  380. dput(dentry);
  381. return error;
  382. }
  383. int
  384. xfs_attrmulti_attr_get(
  385. struct inode *inode,
  386. unsigned char *name,
  387. unsigned char __user *ubuf,
  388. __uint32_t *len,
  389. __uint32_t flags)
  390. {
  391. unsigned char *kbuf;
  392. int error = EFAULT;
  393. if (*len > XATTR_SIZE_MAX)
  394. return EINVAL;
  395. kbuf = kmem_zalloc(*len, KM_SLEEP | KM_MAYFAIL);
  396. if (!kbuf) {
  397. kbuf = kmem_zalloc_large(*len);
  398. if (!kbuf)
  399. return ENOMEM;
  400. }
  401. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  402. if (error)
  403. goto out_kfree;
  404. if (copy_to_user(ubuf, kbuf, *len))
  405. error = EFAULT;
  406. out_kfree:
  407. if (is_vmalloc_addr(kbuf))
  408. kmem_free_large(kbuf);
  409. else
  410. kmem_free(kbuf);
  411. return error;
  412. }
  413. int
  414. xfs_attrmulti_attr_set(
  415. struct inode *inode,
  416. unsigned char *name,
  417. const unsigned char __user *ubuf,
  418. __uint32_t len,
  419. __uint32_t flags)
  420. {
  421. unsigned char *kbuf;
  422. int error = EFAULT;
  423. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  424. return EPERM;
  425. if (len > XATTR_SIZE_MAX)
  426. return EINVAL;
  427. kbuf = memdup_user(ubuf, len);
  428. if (IS_ERR(kbuf))
  429. return PTR_ERR(kbuf);
  430. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  431. return error;
  432. }
  433. int
  434. xfs_attrmulti_attr_remove(
  435. struct inode *inode,
  436. unsigned char *name,
  437. __uint32_t flags)
  438. {
  439. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  440. return EPERM;
  441. return xfs_attr_remove(XFS_I(inode), name, flags);
  442. }
  443. STATIC int
  444. xfs_attrmulti_by_handle(
  445. struct file *parfilp,
  446. void __user *arg)
  447. {
  448. int error;
  449. xfs_attr_multiop_t *ops;
  450. xfs_fsop_attrmulti_handlereq_t am_hreq;
  451. struct dentry *dentry;
  452. unsigned int i, size;
  453. unsigned char *attr_name;
  454. if (!capable(CAP_SYS_ADMIN))
  455. return -XFS_ERROR(EPERM);
  456. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  457. return -XFS_ERROR(EFAULT);
  458. /* overflow check */
  459. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  460. return -E2BIG;
  461. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  462. if (IS_ERR(dentry))
  463. return PTR_ERR(dentry);
  464. error = E2BIG;
  465. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  466. if (!size || size > 16 * PAGE_SIZE)
  467. goto out_dput;
  468. ops = memdup_user(am_hreq.ops, size);
  469. if (IS_ERR(ops)) {
  470. error = PTR_ERR(ops);
  471. goto out_dput;
  472. }
  473. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  474. if (!attr_name)
  475. goto out_kfree_ops;
  476. error = 0;
  477. for (i = 0; i < am_hreq.opcount; i++) {
  478. ops[i].am_error = strncpy_from_user((char *)attr_name,
  479. ops[i].am_attrname, MAXNAMELEN);
  480. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  481. error = -ERANGE;
  482. if (ops[i].am_error < 0)
  483. break;
  484. switch (ops[i].am_opcode) {
  485. case ATTR_OP_GET:
  486. ops[i].am_error = xfs_attrmulti_attr_get(
  487. dentry->d_inode, attr_name,
  488. ops[i].am_attrvalue, &ops[i].am_length,
  489. ops[i].am_flags);
  490. break;
  491. case ATTR_OP_SET:
  492. ops[i].am_error = mnt_want_write_file(parfilp);
  493. if (ops[i].am_error)
  494. break;
  495. ops[i].am_error = xfs_attrmulti_attr_set(
  496. dentry->d_inode, attr_name,
  497. ops[i].am_attrvalue, ops[i].am_length,
  498. ops[i].am_flags);
  499. mnt_drop_write_file(parfilp);
  500. break;
  501. case ATTR_OP_REMOVE:
  502. ops[i].am_error = mnt_want_write_file(parfilp);
  503. if (ops[i].am_error)
  504. break;
  505. ops[i].am_error = xfs_attrmulti_attr_remove(
  506. dentry->d_inode, attr_name,
  507. ops[i].am_flags);
  508. mnt_drop_write_file(parfilp);
  509. break;
  510. default:
  511. ops[i].am_error = EINVAL;
  512. }
  513. }
  514. if (copy_to_user(am_hreq.ops, ops, size))
  515. error = XFS_ERROR(EFAULT);
  516. kfree(attr_name);
  517. out_kfree_ops:
  518. kfree(ops);
  519. out_dput:
  520. dput(dentry);
  521. return -error;
  522. }
  523. int
  524. xfs_ioc_space(
  525. struct xfs_inode *ip,
  526. struct inode *inode,
  527. struct file *filp,
  528. int ioflags,
  529. unsigned int cmd,
  530. xfs_flock64_t *bf)
  531. {
  532. int attr_flags = 0;
  533. int error;
  534. /*
  535. * Only allow the sys admin to reserve space unless
  536. * unwritten extents are enabled.
  537. */
  538. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  539. !capable(CAP_SYS_ADMIN))
  540. return -XFS_ERROR(EPERM);
  541. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  542. return -XFS_ERROR(EPERM);
  543. if (!(filp->f_mode & FMODE_WRITE))
  544. return -XFS_ERROR(EBADF);
  545. if (!S_ISREG(inode->i_mode))
  546. return -XFS_ERROR(EINVAL);
  547. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  548. attr_flags |= XFS_ATTR_NONBLOCK;
  549. if (filp->f_flags & O_DSYNC)
  550. attr_flags |= XFS_ATTR_SYNC;
  551. if (ioflags & IO_INVIS)
  552. attr_flags |= XFS_ATTR_DMI;
  553. error = mnt_want_write_file(filp);
  554. if (error)
  555. return error;
  556. error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
  557. mnt_drop_write_file(filp);
  558. return -error;
  559. }
  560. STATIC int
  561. xfs_ioc_bulkstat(
  562. xfs_mount_t *mp,
  563. unsigned int cmd,
  564. void __user *arg)
  565. {
  566. xfs_fsop_bulkreq_t bulkreq;
  567. int count; /* # of records returned */
  568. xfs_ino_t inlast; /* last inode number */
  569. int done;
  570. int error;
  571. /* done = 1 if there are more stats to get and if bulkstat */
  572. /* should be called again (unused here, but used in dmapi) */
  573. if (!capable(CAP_SYS_ADMIN))
  574. return -EPERM;
  575. if (XFS_FORCED_SHUTDOWN(mp))
  576. return -XFS_ERROR(EIO);
  577. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  578. return -XFS_ERROR(EFAULT);
  579. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  580. return -XFS_ERROR(EFAULT);
  581. if ((count = bulkreq.icount) <= 0)
  582. return -XFS_ERROR(EINVAL);
  583. if (bulkreq.ubuffer == NULL)
  584. return -XFS_ERROR(EINVAL);
  585. if (cmd == XFS_IOC_FSINUMBERS)
  586. error = xfs_inumbers(mp, &inlast, &count,
  587. bulkreq.ubuffer, xfs_inumbers_fmt);
  588. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  589. error = xfs_bulkstat_single(mp, &inlast,
  590. bulkreq.ubuffer, &done);
  591. else /* XFS_IOC_FSBULKSTAT */
  592. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  593. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  594. &done);
  595. if (error)
  596. return -error;
  597. if (bulkreq.ocount != NULL) {
  598. if (copy_to_user(bulkreq.lastip, &inlast,
  599. sizeof(xfs_ino_t)))
  600. return -XFS_ERROR(EFAULT);
  601. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  602. return -XFS_ERROR(EFAULT);
  603. }
  604. return 0;
  605. }
  606. STATIC int
  607. xfs_ioc_fsgeometry_v1(
  608. xfs_mount_t *mp,
  609. void __user *arg)
  610. {
  611. xfs_fsop_geom_t fsgeo;
  612. int error;
  613. error = xfs_fs_geometry(mp, &fsgeo, 3);
  614. if (error)
  615. return -error;
  616. /*
  617. * Caller should have passed an argument of type
  618. * xfs_fsop_geom_v1_t. This is a proper subset of the
  619. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  620. */
  621. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  622. return -XFS_ERROR(EFAULT);
  623. return 0;
  624. }
  625. STATIC int
  626. xfs_ioc_fsgeometry(
  627. xfs_mount_t *mp,
  628. void __user *arg)
  629. {
  630. xfs_fsop_geom_t fsgeo;
  631. int error;
  632. error = xfs_fs_geometry(mp, &fsgeo, 4);
  633. if (error)
  634. return -error;
  635. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  636. return -XFS_ERROR(EFAULT);
  637. return 0;
  638. }
  639. /*
  640. * Linux extended inode flags interface.
  641. */
  642. STATIC unsigned int
  643. xfs_merge_ioc_xflags(
  644. unsigned int flags,
  645. unsigned int start)
  646. {
  647. unsigned int xflags = start;
  648. if (flags & FS_IMMUTABLE_FL)
  649. xflags |= XFS_XFLAG_IMMUTABLE;
  650. else
  651. xflags &= ~XFS_XFLAG_IMMUTABLE;
  652. if (flags & FS_APPEND_FL)
  653. xflags |= XFS_XFLAG_APPEND;
  654. else
  655. xflags &= ~XFS_XFLAG_APPEND;
  656. if (flags & FS_SYNC_FL)
  657. xflags |= XFS_XFLAG_SYNC;
  658. else
  659. xflags &= ~XFS_XFLAG_SYNC;
  660. if (flags & FS_NOATIME_FL)
  661. xflags |= XFS_XFLAG_NOATIME;
  662. else
  663. xflags &= ~XFS_XFLAG_NOATIME;
  664. if (flags & FS_NODUMP_FL)
  665. xflags |= XFS_XFLAG_NODUMP;
  666. else
  667. xflags &= ~XFS_XFLAG_NODUMP;
  668. return xflags;
  669. }
  670. STATIC unsigned int
  671. xfs_di2lxflags(
  672. __uint16_t di_flags)
  673. {
  674. unsigned int flags = 0;
  675. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  676. flags |= FS_IMMUTABLE_FL;
  677. if (di_flags & XFS_DIFLAG_APPEND)
  678. flags |= FS_APPEND_FL;
  679. if (di_flags & XFS_DIFLAG_SYNC)
  680. flags |= FS_SYNC_FL;
  681. if (di_flags & XFS_DIFLAG_NOATIME)
  682. flags |= FS_NOATIME_FL;
  683. if (di_flags & XFS_DIFLAG_NODUMP)
  684. flags |= FS_NODUMP_FL;
  685. return flags;
  686. }
  687. STATIC int
  688. xfs_ioc_fsgetxattr(
  689. xfs_inode_t *ip,
  690. int attr,
  691. void __user *arg)
  692. {
  693. struct fsxattr fa;
  694. memset(&fa, 0, sizeof(struct fsxattr));
  695. xfs_ilock(ip, XFS_ILOCK_SHARED);
  696. fa.fsx_xflags = xfs_ip2xflags(ip);
  697. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  698. fa.fsx_projid = xfs_get_projid(ip);
  699. if (attr) {
  700. if (ip->i_afp) {
  701. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  702. fa.fsx_nextents = ip->i_afp->if_bytes /
  703. sizeof(xfs_bmbt_rec_t);
  704. else
  705. fa.fsx_nextents = ip->i_d.di_anextents;
  706. } else
  707. fa.fsx_nextents = 0;
  708. } else {
  709. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  710. fa.fsx_nextents = ip->i_df.if_bytes /
  711. sizeof(xfs_bmbt_rec_t);
  712. else
  713. fa.fsx_nextents = ip->i_d.di_nextents;
  714. }
  715. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  716. if (copy_to_user(arg, &fa, sizeof(fa)))
  717. return -EFAULT;
  718. return 0;
  719. }
  720. STATIC void
  721. xfs_set_diflags(
  722. struct xfs_inode *ip,
  723. unsigned int xflags)
  724. {
  725. unsigned int di_flags;
  726. /* can't set PREALLOC this way, just preserve it */
  727. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  728. if (xflags & XFS_XFLAG_IMMUTABLE)
  729. di_flags |= XFS_DIFLAG_IMMUTABLE;
  730. if (xflags & XFS_XFLAG_APPEND)
  731. di_flags |= XFS_DIFLAG_APPEND;
  732. if (xflags & XFS_XFLAG_SYNC)
  733. di_flags |= XFS_DIFLAG_SYNC;
  734. if (xflags & XFS_XFLAG_NOATIME)
  735. di_flags |= XFS_DIFLAG_NOATIME;
  736. if (xflags & XFS_XFLAG_NODUMP)
  737. di_flags |= XFS_DIFLAG_NODUMP;
  738. if (xflags & XFS_XFLAG_PROJINHERIT)
  739. di_flags |= XFS_DIFLAG_PROJINHERIT;
  740. if (xflags & XFS_XFLAG_NODEFRAG)
  741. di_flags |= XFS_DIFLAG_NODEFRAG;
  742. if (xflags & XFS_XFLAG_FILESTREAM)
  743. di_flags |= XFS_DIFLAG_FILESTREAM;
  744. if (S_ISDIR(ip->i_d.di_mode)) {
  745. if (xflags & XFS_XFLAG_RTINHERIT)
  746. di_flags |= XFS_DIFLAG_RTINHERIT;
  747. if (xflags & XFS_XFLAG_NOSYMLINKS)
  748. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  749. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  750. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  751. } else if (S_ISREG(ip->i_d.di_mode)) {
  752. if (xflags & XFS_XFLAG_REALTIME)
  753. di_flags |= XFS_DIFLAG_REALTIME;
  754. if (xflags & XFS_XFLAG_EXTSIZE)
  755. di_flags |= XFS_DIFLAG_EXTSIZE;
  756. }
  757. ip->i_d.di_flags = di_flags;
  758. }
  759. STATIC void
  760. xfs_diflags_to_linux(
  761. struct xfs_inode *ip)
  762. {
  763. struct inode *inode = VFS_I(ip);
  764. unsigned int xflags = xfs_ip2xflags(ip);
  765. if (xflags & XFS_XFLAG_IMMUTABLE)
  766. inode->i_flags |= S_IMMUTABLE;
  767. else
  768. inode->i_flags &= ~S_IMMUTABLE;
  769. if (xflags & XFS_XFLAG_APPEND)
  770. inode->i_flags |= S_APPEND;
  771. else
  772. inode->i_flags &= ~S_APPEND;
  773. if (xflags & XFS_XFLAG_SYNC)
  774. inode->i_flags |= S_SYNC;
  775. else
  776. inode->i_flags &= ~S_SYNC;
  777. if (xflags & XFS_XFLAG_NOATIME)
  778. inode->i_flags |= S_NOATIME;
  779. else
  780. inode->i_flags &= ~S_NOATIME;
  781. }
  782. #define FSX_PROJID 1
  783. #define FSX_EXTSIZE 2
  784. #define FSX_XFLAGS 4
  785. #define FSX_NONBLOCK 8
  786. STATIC int
  787. xfs_ioctl_setattr(
  788. xfs_inode_t *ip,
  789. struct fsxattr *fa,
  790. int mask)
  791. {
  792. struct xfs_mount *mp = ip->i_mount;
  793. struct xfs_trans *tp;
  794. unsigned int lock_flags = 0;
  795. struct xfs_dquot *udqp = NULL;
  796. struct xfs_dquot *gdqp = NULL;
  797. struct xfs_dquot *olddquot = NULL;
  798. int code;
  799. trace_xfs_ioctl_setattr(ip);
  800. if (mp->m_flags & XFS_MOUNT_RDONLY)
  801. return XFS_ERROR(EROFS);
  802. if (XFS_FORCED_SHUTDOWN(mp))
  803. return XFS_ERROR(EIO);
  804. /*
  805. * Disallow 32bit project ids when projid32bit feature is not enabled.
  806. */
  807. if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) &&
  808. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  809. return XFS_ERROR(EINVAL);
  810. /*
  811. * If disk quotas is on, we make sure that the dquots do exist on disk,
  812. * before we start any other transactions. Trying to do this later
  813. * is messy. We don't care to take a readlock to look at the ids
  814. * in inode here, because we can't hold it across the trans_reserve.
  815. * If the IDs do change before we take the ilock, we're covered
  816. * because the i_*dquot fields will get updated anyway.
  817. */
  818. if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
  819. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  820. ip->i_d.di_gid, fa->fsx_projid,
  821. XFS_QMOPT_PQUOTA, &udqp, &gdqp);
  822. if (code)
  823. return code;
  824. }
  825. /*
  826. * For the other attributes, we acquire the inode lock and
  827. * first do an error checking pass.
  828. */
  829. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  830. code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  831. if (code)
  832. goto error_return;
  833. lock_flags = XFS_ILOCK_EXCL;
  834. xfs_ilock(ip, lock_flags);
  835. /*
  836. * CAP_FOWNER overrides the following restrictions:
  837. *
  838. * The user ID of the calling process must be equal
  839. * to the file owner ID, except in cases where the
  840. * CAP_FSETID capability is applicable.
  841. */
  842. if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
  843. code = XFS_ERROR(EPERM);
  844. goto error_return;
  845. }
  846. /*
  847. * Do a quota reservation only if projid is actually going to change.
  848. */
  849. if (mask & FSX_PROJID) {
  850. if (XFS_IS_QUOTA_RUNNING(mp) &&
  851. XFS_IS_PQUOTA_ON(mp) &&
  852. xfs_get_projid(ip) != fa->fsx_projid) {
  853. ASSERT(tp);
  854. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  855. capable(CAP_FOWNER) ?
  856. XFS_QMOPT_FORCE_RES : 0);
  857. if (code) /* out of quota */
  858. goto error_return;
  859. }
  860. }
  861. if (mask & FSX_EXTSIZE) {
  862. /*
  863. * Can't change extent size if any extents are allocated.
  864. */
  865. if (ip->i_d.di_nextents &&
  866. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  867. fa->fsx_extsize)) {
  868. code = XFS_ERROR(EINVAL); /* EFBIG? */
  869. goto error_return;
  870. }
  871. /*
  872. * Extent size must be a multiple of the appropriate block
  873. * size, if set at all. It must also be smaller than the
  874. * maximum extent size supported by the filesystem.
  875. *
  876. * Also, for non-realtime files, limit the extent size hint to
  877. * half the size of the AGs in the filesystem so alignment
  878. * doesn't result in extents larger than an AG.
  879. */
  880. if (fa->fsx_extsize != 0) {
  881. xfs_extlen_t size;
  882. xfs_fsblock_t extsize_fsb;
  883. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  884. if (extsize_fsb > MAXEXTLEN) {
  885. code = XFS_ERROR(EINVAL);
  886. goto error_return;
  887. }
  888. if (XFS_IS_REALTIME_INODE(ip) ||
  889. ((mask & FSX_XFLAGS) &&
  890. (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
  891. size = mp->m_sb.sb_rextsize <<
  892. mp->m_sb.sb_blocklog;
  893. } else {
  894. size = mp->m_sb.sb_blocksize;
  895. if (extsize_fsb > mp->m_sb.sb_agblocks / 2) {
  896. code = XFS_ERROR(EINVAL);
  897. goto error_return;
  898. }
  899. }
  900. if (fa->fsx_extsize % size) {
  901. code = XFS_ERROR(EINVAL);
  902. goto error_return;
  903. }
  904. }
  905. }
  906. if (mask & FSX_XFLAGS) {
  907. /*
  908. * Can't change realtime flag if any extents are allocated.
  909. */
  910. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  911. (XFS_IS_REALTIME_INODE(ip)) !=
  912. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  913. code = XFS_ERROR(EINVAL); /* EFBIG? */
  914. goto error_return;
  915. }
  916. /*
  917. * If realtime flag is set then must have realtime data.
  918. */
  919. if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  920. if ((mp->m_sb.sb_rblocks == 0) ||
  921. (mp->m_sb.sb_rextsize == 0) ||
  922. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  923. code = XFS_ERROR(EINVAL);
  924. goto error_return;
  925. }
  926. }
  927. /*
  928. * Can't modify an immutable/append-only file unless
  929. * we have appropriate permission.
  930. */
  931. if ((ip->i_d.di_flags &
  932. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  933. (fa->fsx_xflags &
  934. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  935. !capable(CAP_LINUX_IMMUTABLE)) {
  936. code = XFS_ERROR(EPERM);
  937. goto error_return;
  938. }
  939. }
  940. xfs_trans_ijoin(tp, ip, 0);
  941. /*
  942. * Change file ownership. Must be the owner or privileged.
  943. */
  944. if (mask & FSX_PROJID) {
  945. /*
  946. * CAP_FSETID overrides the following restrictions:
  947. *
  948. * The set-user-ID and set-group-ID bits of a file will be
  949. * cleared upon successful return from chown()
  950. */
  951. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  952. !capable(CAP_FSETID))
  953. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  954. /*
  955. * Change the ownerships and register quota modifications
  956. * in the transaction.
  957. */
  958. if (xfs_get_projid(ip) != fa->fsx_projid) {
  959. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  960. olddquot = xfs_qm_vop_chown(tp, ip,
  961. &ip->i_gdquot, gdqp);
  962. }
  963. xfs_set_projid(ip, fa->fsx_projid);
  964. /*
  965. * We may have to rev the inode as well as
  966. * the superblock version number since projids didn't
  967. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  968. */
  969. if (ip->i_d.di_version == 1)
  970. xfs_bump_ino_vers2(tp, ip);
  971. }
  972. }
  973. if (mask & FSX_EXTSIZE)
  974. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  975. if (mask & FSX_XFLAGS) {
  976. xfs_set_diflags(ip, fa->fsx_xflags);
  977. xfs_diflags_to_linux(ip);
  978. }
  979. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  980. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  981. XFS_STATS_INC(xs_ig_attrchg);
  982. /*
  983. * If this is a synchronous mount, make sure that the
  984. * transaction goes to disk before returning to the user.
  985. * This is slightly sub-optimal in that truncates require
  986. * two sync transactions instead of one for wsync filesystems.
  987. * One for the truncate and one for the timestamps since we
  988. * don't want to change the timestamps unless we're sure the
  989. * truncate worked. Truncates are less than 1% of the laddis
  990. * mix so this probably isn't worth the trouble to optimize.
  991. */
  992. if (mp->m_flags & XFS_MOUNT_WSYNC)
  993. xfs_trans_set_sync(tp);
  994. code = xfs_trans_commit(tp, 0);
  995. xfs_iunlock(ip, lock_flags);
  996. /*
  997. * Release any dquot(s) the inode had kept before chown.
  998. */
  999. xfs_qm_dqrele(olddquot);
  1000. xfs_qm_dqrele(udqp);
  1001. xfs_qm_dqrele(gdqp);
  1002. return code;
  1003. error_return:
  1004. xfs_qm_dqrele(udqp);
  1005. xfs_qm_dqrele(gdqp);
  1006. xfs_trans_cancel(tp, 0);
  1007. if (lock_flags)
  1008. xfs_iunlock(ip, lock_flags);
  1009. return code;
  1010. }
  1011. STATIC int
  1012. xfs_ioc_fssetxattr(
  1013. xfs_inode_t *ip,
  1014. struct file *filp,
  1015. void __user *arg)
  1016. {
  1017. struct fsxattr fa;
  1018. unsigned int mask;
  1019. int error;
  1020. if (copy_from_user(&fa, arg, sizeof(fa)))
  1021. return -EFAULT;
  1022. mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
  1023. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1024. mask |= FSX_NONBLOCK;
  1025. error = mnt_want_write_file(filp);
  1026. if (error)
  1027. return error;
  1028. error = xfs_ioctl_setattr(ip, &fa, mask);
  1029. mnt_drop_write_file(filp);
  1030. return -error;
  1031. }
  1032. STATIC int
  1033. xfs_ioc_getxflags(
  1034. xfs_inode_t *ip,
  1035. void __user *arg)
  1036. {
  1037. unsigned int flags;
  1038. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1039. if (copy_to_user(arg, &flags, sizeof(flags)))
  1040. return -EFAULT;
  1041. return 0;
  1042. }
  1043. STATIC int
  1044. xfs_ioc_setxflags(
  1045. xfs_inode_t *ip,
  1046. struct file *filp,
  1047. void __user *arg)
  1048. {
  1049. struct fsxattr fa;
  1050. unsigned int flags;
  1051. unsigned int mask;
  1052. int error;
  1053. if (copy_from_user(&flags, arg, sizeof(flags)))
  1054. return -EFAULT;
  1055. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1056. FS_NOATIME_FL | FS_NODUMP_FL | \
  1057. FS_SYNC_FL))
  1058. return -EOPNOTSUPP;
  1059. mask = FSX_XFLAGS;
  1060. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1061. mask |= FSX_NONBLOCK;
  1062. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1063. error = mnt_want_write_file(filp);
  1064. if (error)
  1065. return error;
  1066. error = xfs_ioctl_setattr(ip, &fa, mask);
  1067. mnt_drop_write_file(filp);
  1068. return -error;
  1069. }
  1070. STATIC int
  1071. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1072. {
  1073. struct getbmap __user *base = *ap;
  1074. /* copy only getbmap portion (not getbmapx) */
  1075. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1076. return XFS_ERROR(EFAULT);
  1077. *ap += sizeof(struct getbmap);
  1078. return 0;
  1079. }
  1080. STATIC int
  1081. xfs_ioc_getbmap(
  1082. struct xfs_inode *ip,
  1083. int ioflags,
  1084. unsigned int cmd,
  1085. void __user *arg)
  1086. {
  1087. struct getbmapx bmx;
  1088. int error;
  1089. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1090. return -XFS_ERROR(EFAULT);
  1091. if (bmx.bmv_count < 2)
  1092. return -XFS_ERROR(EINVAL);
  1093. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1094. if (ioflags & IO_INVIS)
  1095. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1096. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1097. (struct getbmap *)arg+1);
  1098. if (error)
  1099. return -error;
  1100. /* copy back header - only size of getbmap */
  1101. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1102. return -XFS_ERROR(EFAULT);
  1103. return 0;
  1104. }
  1105. STATIC int
  1106. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1107. {
  1108. struct getbmapx __user *base = *ap;
  1109. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1110. return XFS_ERROR(EFAULT);
  1111. *ap += sizeof(struct getbmapx);
  1112. return 0;
  1113. }
  1114. STATIC int
  1115. xfs_ioc_getbmapx(
  1116. struct xfs_inode *ip,
  1117. void __user *arg)
  1118. {
  1119. struct getbmapx bmx;
  1120. int error;
  1121. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1122. return -XFS_ERROR(EFAULT);
  1123. if (bmx.bmv_count < 2)
  1124. return -XFS_ERROR(EINVAL);
  1125. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1126. return -XFS_ERROR(EINVAL);
  1127. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1128. (struct getbmapx *)arg+1);
  1129. if (error)
  1130. return -error;
  1131. /* copy back header */
  1132. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1133. return -XFS_ERROR(EFAULT);
  1134. return 0;
  1135. }
  1136. /*
  1137. * Note: some of the ioctl's return positive numbers as a
  1138. * byte count indicating success, such as readlink_by_handle.
  1139. * So we don't "sign flip" like most other routines. This means
  1140. * true errors need to be returned as a negative value.
  1141. */
  1142. long
  1143. xfs_file_ioctl(
  1144. struct file *filp,
  1145. unsigned int cmd,
  1146. unsigned long p)
  1147. {
  1148. struct inode *inode = file_inode(filp);
  1149. struct xfs_inode *ip = XFS_I(inode);
  1150. struct xfs_mount *mp = ip->i_mount;
  1151. void __user *arg = (void __user *)p;
  1152. int ioflags = 0;
  1153. int error;
  1154. if (filp->f_mode & FMODE_NOCMTIME)
  1155. ioflags |= IO_INVIS;
  1156. trace_xfs_file_ioctl(ip);
  1157. switch (cmd) {
  1158. case FITRIM:
  1159. return xfs_ioc_trim(mp, arg);
  1160. case XFS_IOC_ALLOCSP:
  1161. case XFS_IOC_FREESP:
  1162. case XFS_IOC_RESVSP:
  1163. case XFS_IOC_UNRESVSP:
  1164. case XFS_IOC_ALLOCSP64:
  1165. case XFS_IOC_FREESP64:
  1166. case XFS_IOC_RESVSP64:
  1167. case XFS_IOC_UNRESVSP64:
  1168. case XFS_IOC_ZERO_RANGE: {
  1169. xfs_flock64_t bf;
  1170. if (copy_from_user(&bf, arg, sizeof(bf)))
  1171. return -XFS_ERROR(EFAULT);
  1172. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1173. }
  1174. case XFS_IOC_DIOINFO: {
  1175. struct dioattr da;
  1176. xfs_buftarg_t *target =
  1177. XFS_IS_REALTIME_INODE(ip) ?
  1178. mp->m_rtdev_targp : mp->m_ddev_targp;
  1179. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  1180. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1181. if (copy_to_user(arg, &da, sizeof(da)))
  1182. return -XFS_ERROR(EFAULT);
  1183. return 0;
  1184. }
  1185. case XFS_IOC_FSBULKSTAT_SINGLE:
  1186. case XFS_IOC_FSBULKSTAT:
  1187. case XFS_IOC_FSINUMBERS:
  1188. return xfs_ioc_bulkstat(mp, cmd, arg);
  1189. case XFS_IOC_FSGEOMETRY_V1:
  1190. return xfs_ioc_fsgeometry_v1(mp, arg);
  1191. case XFS_IOC_FSGEOMETRY:
  1192. return xfs_ioc_fsgeometry(mp, arg);
  1193. case XFS_IOC_GETVERSION:
  1194. return put_user(inode->i_generation, (int __user *)arg);
  1195. case XFS_IOC_FSGETXATTR:
  1196. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1197. case XFS_IOC_FSGETXATTRA:
  1198. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1199. case XFS_IOC_FSSETXATTR:
  1200. return xfs_ioc_fssetxattr(ip, filp, arg);
  1201. case XFS_IOC_GETXFLAGS:
  1202. return xfs_ioc_getxflags(ip, arg);
  1203. case XFS_IOC_SETXFLAGS:
  1204. return xfs_ioc_setxflags(ip, filp, arg);
  1205. case XFS_IOC_FSSETDM: {
  1206. struct fsdmidata dmi;
  1207. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1208. return -XFS_ERROR(EFAULT);
  1209. error = mnt_want_write_file(filp);
  1210. if (error)
  1211. return error;
  1212. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1213. dmi.fsd_dmstate);
  1214. mnt_drop_write_file(filp);
  1215. return -error;
  1216. }
  1217. case XFS_IOC_GETBMAP:
  1218. case XFS_IOC_GETBMAPA:
  1219. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1220. case XFS_IOC_GETBMAPX:
  1221. return xfs_ioc_getbmapx(ip, arg);
  1222. case XFS_IOC_FD_TO_HANDLE:
  1223. case XFS_IOC_PATH_TO_HANDLE:
  1224. case XFS_IOC_PATH_TO_FSHANDLE: {
  1225. xfs_fsop_handlereq_t hreq;
  1226. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1227. return -XFS_ERROR(EFAULT);
  1228. return xfs_find_handle(cmd, &hreq);
  1229. }
  1230. case XFS_IOC_OPEN_BY_HANDLE: {
  1231. xfs_fsop_handlereq_t hreq;
  1232. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1233. return -XFS_ERROR(EFAULT);
  1234. return xfs_open_by_handle(filp, &hreq);
  1235. }
  1236. case XFS_IOC_FSSETDM_BY_HANDLE:
  1237. return xfs_fssetdm_by_handle(filp, arg);
  1238. case XFS_IOC_READLINK_BY_HANDLE: {
  1239. xfs_fsop_handlereq_t hreq;
  1240. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1241. return -XFS_ERROR(EFAULT);
  1242. return xfs_readlink_by_handle(filp, &hreq);
  1243. }
  1244. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1245. return xfs_attrlist_by_handle(filp, arg);
  1246. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1247. return xfs_attrmulti_by_handle(filp, arg);
  1248. case XFS_IOC_SWAPEXT: {
  1249. struct xfs_swapext sxp;
  1250. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1251. return -XFS_ERROR(EFAULT);
  1252. error = mnt_want_write_file(filp);
  1253. if (error)
  1254. return error;
  1255. error = xfs_swapext(&sxp);
  1256. mnt_drop_write_file(filp);
  1257. return -error;
  1258. }
  1259. case XFS_IOC_FSCOUNTS: {
  1260. xfs_fsop_counts_t out;
  1261. error = xfs_fs_counts(mp, &out);
  1262. if (error)
  1263. return -error;
  1264. if (copy_to_user(arg, &out, sizeof(out)))
  1265. return -XFS_ERROR(EFAULT);
  1266. return 0;
  1267. }
  1268. case XFS_IOC_SET_RESBLKS: {
  1269. xfs_fsop_resblks_t inout;
  1270. __uint64_t in;
  1271. if (!capable(CAP_SYS_ADMIN))
  1272. return -EPERM;
  1273. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1274. return -XFS_ERROR(EROFS);
  1275. if (copy_from_user(&inout, arg, sizeof(inout)))
  1276. return -XFS_ERROR(EFAULT);
  1277. error = mnt_want_write_file(filp);
  1278. if (error)
  1279. return error;
  1280. /* input parameter is passed in resblks field of structure */
  1281. in = inout.resblks;
  1282. error = xfs_reserve_blocks(mp, &in, &inout);
  1283. mnt_drop_write_file(filp);
  1284. if (error)
  1285. return -error;
  1286. if (copy_to_user(arg, &inout, sizeof(inout)))
  1287. return -XFS_ERROR(EFAULT);
  1288. return 0;
  1289. }
  1290. case XFS_IOC_GET_RESBLKS: {
  1291. xfs_fsop_resblks_t out;
  1292. if (!capable(CAP_SYS_ADMIN))
  1293. return -EPERM;
  1294. error = xfs_reserve_blocks(mp, NULL, &out);
  1295. if (error)
  1296. return -error;
  1297. if (copy_to_user(arg, &out, sizeof(out)))
  1298. return -XFS_ERROR(EFAULT);
  1299. return 0;
  1300. }
  1301. case XFS_IOC_FSGROWFSDATA: {
  1302. xfs_growfs_data_t in;
  1303. if (copy_from_user(&in, arg, sizeof(in)))
  1304. return -XFS_ERROR(EFAULT);
  1305. error = mnt_want_write_file(filp);
  1306. if (error)
  1307. return error;
  1308. error = xfs_growfs_data(mp, &in);
  1309. mnt_drop_write_file(filp);
  1310. return -error;
  1311. }
  1312. case XFS_IOC_FSGROWFSLOG: {
  1313. xfs_growfs_log_t in;
  1314. if (copy_from_user(&in, arg, sizeof(in)))
  1315. return -XFS_ERROR(EFAULT);
  1316. error = mnt_want_write_file(filp);
  1317. if (error)
  1318. return error;
  1319. error = xfs_growfs_log(mp, &in);
  1320. mnt_drop_write_file(filp);
  1321. return -error;
  1322. }
  1323. case XFS_IOC_FSGROWFSRT: {
  1324. xfs_growfs_rt_t in;
  1325. if (copy_from_user(&in, arg, sizeof(in)))
  1326. return -XFS_ERROR(EFAULT);
  1327. error = mnt_want_write_file(filp);
  1328. if (error)
  1329. return error;
  1330. error = xfs_growfs_rt(mp, &in);
  1331. mnt_drop_write_file(filp);
  1332. return -error;
  1333. }
  1334. case XFS_IOC_GOINGDOWN: {
  1335. __uint32_t in;
  1336. if (!capable(CAP_SYS_ADMIN))
  1337. return -EPERM;
  1338. if (get_user(in, (__uint32_t __user *)arg))
  1339. return -XFS_ERROR(EFAULT);
  1340. error = xfs_fs_goingdown(mp, in);
  1341. return -error;
  1342. }
  1343. case XFS_IOC_ERROR_INJECTION: {
  1344. xfs_error_injection_t in;
  1345. if (!capable(CAP_SYS_ADMIN))
  1346. return -EPERM;
  1347. if (copy_from_user(&in, arg, sizeof(in)))
  1348. return -XFS_ERROR(EFAULT);
  1349. error = xfs_errortag_add(in.errtag, mp);
  1350. return -error;
  1351. }
  1352. case XFS_IOC_ERROR_CLEARALL:
  1353. if (!capable(CAP_SYS_ADMIN))
  1354. return -EPERM;
  1355. error = xfs_errortag_clearall(mp, 1);
  1356. return -error;
  1357. case XFS_IOC_FREE_EOFBLOCKS: {
  1358. struct xfs_eofblocks eofb;
  1359. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1360. return -XFS_ERROR(EFAULT);
  1361. if (eofb.eof_version != XFS_EOFBLOCKS_VERSION)
  1362. return -XFS_ERROR(EINVAL);
  1363. if (eofb.eof_flags & ~XFS_EOF_FLAGS_VALID)
  1364. return -XFS_ERROR(EINVAL);
  1365. if (memchr_inv(&eofb.pad32, 0, sizeof(eofb.pad32)) ||
  1366. memchr_inv(eofb.pad64, 0, sizeof(eofb.pad64)))
  1367. return -XFS_ERROR(EINVAL);
  1368. error = xfs_icache_free_eofblocks(mp, &eofb);
  1369. return -error;
  1370. }
  1371. default:
  1372. return -ENOTTY;
  1373. }
  1374. }