xfs_ioctl.c 35 KB

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