xfs_ioctl.c 41 KB

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