xfs_ioctl.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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_mount.h"
  25. #include "xfs_inode.h"
  26. #include "xfs_ioctl.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_rtalloc.h"
  29. #include "xfs_itable.h"
  30. #include "xfs_error.h"
  31. #include "xfs_attr.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_bmap_util.h"
  34. #include "xfs_fsops.h"
  35. #include "xfs_discard.h"
  36. #include "xfs_quota.h"
  37. #include "xfs_export.h"
  38. #include "xfs_trace.h"
  39. #include "xfs_icache.h"
  40. #include "xfs_symlink.h"
  41. #include "xfs_trans.h"
  42. #include "xfs_pnfs.h"
  43. #include "xfs_acl.h"
  44. #include "xfs_btree.h"
  45. #include <linux/fsmap.h>
  46. #include "xfs_fsmap.h"
  47. #include <linux/capability.h>
  48. #include <linux/cred.h>
  49. #include <linux/dcache.h>
  50. #include <linux/mount.h>
  51. #include <linux/namei.h>
  52. #include <linux/pagemap.h>
  53. #include <linux/slab.h>
  54. #include <linux/exportfs.h>
  55. /*
  56. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  57. * a file or fs handle.
  58. *
  59. * XFS_IOC_PATH_TO_FSHANDLE
  60. * returns fs handle for a mount point or path within that mount point
  61. * XFS_IOC_FD_TO_HANDLE
  62. * returns full handle for a FD opened in user space
  63. * XFS_IOC_PATH_TO_HANDLE
  64. * returns full handle for a path
  65. */
  66. int
  67. xfs_find_handle(
  68. unsigned int cmd,
  69. xfs_fsop_handlereq_t *hreq)
  70. {
  71. int hsize;
  72. xfs_handle_t handle;
  73. struct inode *inode;
  74. struct fd f = {NULL};
  75. struct path path;
  76. int error;
  77. struct xfs_inode *ip;
  78. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  79. f = fdget(hreq->fd);
  80. if (!f.file)
  81. return -EBADF;
  82. inode = file_inode(f.file);
  83. } else {
  84. error = user_lpath((const char __user *)hreq->path, &path);
  85. if (error)
  86. return error;
  87. inode = d_inode(path.dentry);
  88. }
  89. ip = XFS_I(inode);
  90. /*
  91. * We can only generate handles for inodes residing on a XFS filesystem,
  92. * and only for regular files, directories or symbolic links.
  93. */
  94. error = -EINVAL;
  95. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  96. goto out_put;
  97. error = -EBADF;
  98. if (!S_ISREG(inode->i_mode) &&
  99. !S_ISDIR(inode->i_mode) &&
  100. !S_ISLNK(inode->i_mode))
  101. goto out_put;
  102. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  103. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  104. /*
  105. * This handle only contains an fsid, zero the rest.
  106. */
  107. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  108. hsize = sizeof(xfs_fsid_t);
  109. } else {
  110. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  111. sizeof(handle.ha_fid.fid_len);
  112. handle.ha_fid.fid_pad = 0;
  113. handle.ha_fid.fid_gen = inode->i_generation;
  114. handle.ha_fid.fid_ino = ip->i_ino;
  115. hsize = sizeof(xfs_handle_t);
  116. }
  117. error = -EFAULT;
  118. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  119. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  120. goto out_put;
  121. error = 0;
  122. out_put:
  123. if (cmd == XFS_IOC_FD_TO_HANDLE)
  124. fdput(f);
  125. else
  126. path_put(&path);
  127. return error;
  128. }
  129. /*
  130. * No need to do permission checks on the various pathname components
  131. * as the handle operations are privileged.
  132. */
  133. STATIC int
  134. xfs_handle_acceptable(
  135. void *context,
  136. struct dentry *dentry)
  137. {
  138. return 1;
  139. }
  140. /*
  141. * Convert userspace handle data into a dentry.
  142. */
  143. struct dentry *
  144. xfs_handle_to_dentry(
  145. struct file *parfilp,
  146. void __user *uhandle,
  147. u32 hlen)
  148. {
  149. xfs_handle_t handle;
  150. struct xfs_fid64 fid;
  151. /*
  152. * Only allow handle opens under a directory.
  153. */
  154. if (!S_ISDIR(file_inode(parfilp)->i_mode))
  155. return ERR_PTR(-ENOTDIR);
  156. if (hlen != sizeof(xfs_handle_t))
  157. return ERR_PTR(-EINVAL);
  158. if (copy_from_user(&handle, uhandle, hlen))
  159. return ERR_PTR(-EFAULT);
  160. if (handle.ha_fid.fid_len !=
  161. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  162. return ERR_PTR(-EINVAL);
  163. memset(&fid, 0, sizeof(struct fid));
  164. fid.ino = handle.ha_fid.fid_ino;
  165. fid.gen = handle.ha_fid.fid_gen;
  166. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  167. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  168. xfs_handle_acceptable, NULL);
  169. }
  170. STATIC struct dentry *
  171. xfs_handlereq_to_dentry(
  172. struct file *parfilp,
  173. xfs_fsop_handlereq_t *hreq)
  174. {
  175. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  176. }
  177. int
  178. xfs_open_by_handle(
  179. struct file *parfilp,
  180. xfs_fsop_handlereq_t *hreq)
  181. {
  182. const struct cred *cred = current_cred();
  183. int error;
  184. int fd;
  185. int permflag;
  186. struct file *filp;
  187. struct inode *inode;
  188. struct dentry *dentry;
  189. fmode_t fmode;
  190. struct path path;
  191. if (!capable(CAP_SYS_ADMIN))
  192. return -EPERM;
  193. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  194. if (IS_ERR(dentry))
  195. return PTR_ERR(dentry);
  196. inode = d_inode(dentry);
  197. /* Restrict xfs_open_by_handle to directories & regular files. */
  198. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  199. error = -EPERM;
  200. goto out_dput;
  201. }
  202. #if BITS_PER_LONG != 32
  203. hreq->oflags |= O_LARGEFILE;
  204. #endif
  205. permflag = hreq->oflags;
  206. fmode = OPEN_FMODE(permflag);
  207. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  208. (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
  209. error = -EPERM;
  210. goto out_dput;
  211. }
  212. if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  213. error = -EPERM;
  214. goto out_dput;
  215. }
  216. /* Can't write directories. */
  217. if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
  218. error = -EISDIR;
  219. goto out_dput;
  220. }
  221. fd = get_unused_fd_flags(0);
  222. if (fd < 0) {
  223. error = fd;
  224. goto out_dput;
  225. }
  226. path.mnt = parfilp->f_path.mnt;
  227. path.dentry = dentry;
  228. filp = dentry_open(&path, hreq->oflags, cred);
  229. dput(dentry);
  230. if (IS_ERR(filp)) {
  231. put_unused_fd(fd);
  232. return PTR_ERR(filp);
  233. }
  234. if (S_ISREG(inode->i_mode)) {
  235. filp->f_flags |= O_NOATIME;
  236. filp->f_mode |= FMODE_NOCMTIME;
  237. }
  238. fd_install(fd, filp);
  239. return fd;
  240. out_dput:
  241. dput(dentry);
  242. return error;
  243. }
  244. int
  245. xfs_readlink_by_handle(
  246. struct file *parfilp,
  247. xfs_fsop_handlereq_t *hreq)
  248. {
  249. struct dentry *dentry;
  250. __u32 olen;
  251. int error;
  252. if (!capable(CAP_SYS_ADMIN))
  253. return -EPERM;
  254. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  255. if (IS_ERR(dentry))
  256. return PTR_ERR(dentry);
  257. /* Restrict this handle operation to symlinks only. */
  258. if (!d_is_symlink(dentry)) {
  259. error = -EINVAL;
  260. goto out_dput;
  261. }
  262. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  263. error = -EFAULT;
  264. goto out_dput;
  265. }
  266. error = vfs_readlink(dentry, hreq->ohandle, olen);
  267. out_dput:
  268. dput(dentry);
  269. return error;
  270. }
  271. int
  272. xfs_set_dmattrs(
  273. xfs_inode_t *ip,
  274. u_int evmask,
  275. u_int16_t state)
  276. {
  277. xfs_mount_t *mp = ip->i_mount;
  278. xfs_trans_t *tp;
  279. int error;
  280. if (!capable(CAP_SYS_ADMIN))
  281. return -EPERM;
  282. if (XFS_FORCED_SHUTDOWN(mp))
  283. return -EIO;
  284. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  285. if (error)
  286. return error;
  287. xfs_ilock(ip, XFS_ILOCK_EXCL);
  288. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  289. ip->i_d.di_dmevmask = evmask;
  290. ip->i_d.di_dmstate = state;
  291. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  292. error = xfs_trans_commit(tp);
  293. return error;
  294. }
  295. STATIC int
  296. xfs_fssetdm_by_handle(
  297. struct file *parfilp,
  298. void __user *arg)
  299. {
  300. int error;
  301. struct fsdmidata fsd;
  302. xfs_fsop_setdm_handlereq_t dmhreq;
  303. struct dentry *dentry;
  304. if (!capable(CAP_MKNOD))
  305. return -EPERM;
  306. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  307. return -EFAULT;
  308. error = mnt_want_write_file(parfilp);
  309. if (error)
  310. return error;
  311. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  312. if (IS_ERR(dentry)) {
  313. mnt_drop_write_file(parfilp);
  314. return PTR_ERR(dentry);
  315. }
  316. if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
  317. error = -EPERM;
  318. goto out;
  319. }
  320. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  321. error = -EFAULT;
  322. goto out;
  323. }
  324. error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
  325. fsd.fsd_dmstate);
  326. out:
  327. mnt_drop_write_file(parfilp);
  328. dput(dentry);
  329. return error;
  330. }
  331. STATIC int
  332. xfs_attrlist_by_handle(
  333. struct file *parfilp,
  334. void __user *arg)
  335. {
  336. int error = -ENOMEM;
  337. attrlist_cursor_kern_t *cursor;
  338. struct xfs_fsop_attrlist_handlereq __user *p = arg;
  339. xfs_fsop_attrlist_handlereq_t al_hreq;
  340. struct dentry *dentry;
  341. char *kbuf;
  342. if (!capable(CAP_SYS_ADMIN))
  343. return -EPERM;
  344. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  345. return -EFAULT;
  346. if (al_hreq.buflen < sizeof(struct attrlist) ||
  347. al_hreq.buflen > XFS_XATTR_LIST_MAX)
  348. return -EINVAL;
  349. /*
  350. * Reject flags, only allow namespaces.
  351. */
  352. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  353. return -EINVAL;
  354. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  355. if (IS_ERR(dentry))
  356. return PTR_ERR(dentry);
  357. kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
  358. if (!kbuf)
  359. goto out_dput;
  360. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  361. error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
  362. al_hreq.flags, cursor);
  363. if (error)
  364. goto out_kfree;
  365. if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
  366. error = -EFAULT;
  367. goto out_kfree;
  368. }
  369. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  370. error = -EFAULT;
  371. out_kfree:
  372. kmem_free(kbuf);
  373. out_dput:
  374. dput(dentry);
  375. return error;
  376. }
  377. int
  378. xfs_attrmulti_attr_get(
  379. struct inode *inode,
  380. unsigned char *name,
  381. unsigned char __user *ubuf,
  382. uint32_t *len,
  383. uint32_t flags)
  384. {
  385. unsigned char *kbuf;
  386. int error = -EFAULT;
  387. if (*len > XFS_XATTR_SIZE_MAX)
  388. return -EINVAL;
  389. kbuf = kmem_zalloc_large(*len, KM_SLEEP);
  390. if (!kbuf)
  391. return -ENOMEM;
  392. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  393. if (error)
  394. goto out_kfree;
  395. if (copy_to_user(ubuf, kbuf, *len))
  396. error = -EFAULT;
  397. out_kfree:
  398. kmem_free(kbuf);
  399. return error;
  400. }
  401. int
  402. xfs_attrmulti_attr_set(
  403. struct inode *inode,
  404. unsigned char *name,
  405. const unsigned char __user *ubuf,
  406. uint32_t len,
  407. uint32_t flags)
  408. {
  409. unsigned char *kbuf;
  410. int error;
  411. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  412. return -EPERM;
  413. if (len > XFS_XATTR_SIZE_MAX)
  414. return -EINVAL;
  415. kbuf = memdup_user(ubuf, len);
  416. if (IS_ERR(kbuf))
  417. return PTR_ERR(kbuf);
  418. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  419. if (!error)
  420. xfs_forget_acl(inode, name, flags);
  421. kfree(kbuf);
  422. return error;
  423. }
  424. int
  425. xfs_attrmulti_attr_remove(
  426. struct inode *inode,
  427. unsigned char *name,
  428. uint32_t flags)
  429. {
  430. int error;
  431. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  432. return -EPERM;
  433. error = xfs_attr_remove(XFS_I(inode), name, flags);
  434. if (!error)
  435. xfs_forget_acl(inode, name, flags);
  436. return error;
  437. }
  438. STATIC int
  439. xfs_attrmulti_by_handle(
  440. struct file *parfilp,
  441. void __user *arg)
  442. {
  443. int error;
  444. xfs_attr_multiop_t *ops;
  445. xfs_fsop_attrmulti_handlereq_t am_hreq;
  446. struct dentry *dentry;
  447. unsigned int i, size;
  448. unsigned char *attr_name;
  449. if (!capable(CAP_SYS_ADMIN))
  450. return -EPERM;
  451. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  452. return -EFAULT;
  453. /* overflow check */
  454. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  455. return -E2BIG;
  456. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  457. if (IS_ERR(dentry))
  458. return PTR_ERR(dentry);
  459. error = -E2BIG;
  460. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  461. if (!size || size > 16 * PAGE_SIZE)
  462. goto out_dput;
  463. ops = memdup_user(am_hreq.ops, size);
  464. if (IS_ERR(ops)) {
  465. error = PTR_ERR(ops);
  466. goto out_dput;
  467. }
  468. error = -ENOMEM;
  469. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  470. if (!attr_name)
  471. goto out_kfree_ops;
  472. error = 0;
  473. for (i = 0; i < am_hreq.opcount; i++) {
  474. ops[i].am_error = strncpy_from_user((char *)attr_name,
  475. ops[i].am_attrname, MAXNAMELEN);
  476. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  477. error = -ERANGE;
  478. if (ops[i].am_error < 0)
  479. break;
  480. switch (ops[i].am_opcode) {
  481. case ATTR_OP_GET:
  482. ops[i].am_error = xfs_attrmulti_attr_get(
  483. d_inode(dentry), attr_name,
  484. ops[i].am_attrvalue, &ops[i].am_length,
  485. ops[i].am_flags);
  486. break;
  487. case ATTR_OP_SET:
  488. ops[i].am_error = mnt_want_write_file(parfilp);
  489. if (ops[i].am_error)
  490. break;
  491. ops[i].am_error = xfs_attrmulti_attr_set(
  492. d_inode(dentry), attr_name,
  493. ops[i].am_attrvalue, ops[i].am_length,
  494. ops[i].am_flags);
  495. mnt_drop_write_file(parfilp);
  496. break;
  497. case ATTR_OP_REMOVE:
  498. ops[i].am_error = mnt_want_write_file(parfilp);
  499. if (ops[i].am_error)
  500. break;
  501. ops[i].am_error = xfs_attrmulti_attr_remove(
  502. d_inode(dentry), attr_name,
  503. ops[i].am_flags);
  504. mnt_drop_write_file(parfilp);
  505. break;
  506. default:
  507. ops[i].am_error = -EINVAL;
  508. }
  509. }
  510. if (copy_to_user(am_hreq.ops, ops, size))
  511. error = -EFAULT;
  512. kfree(attr_name);
  513. out_kfree_ops:
  514. kfree(ops);
  515. out_dput:
  516. dput(dentry);
  517. return error;
  518. }
  519. int
  520. xfs_ioc_space(
  521. struct file *filp,
  522. unsigned int cmd,
  523. xfs_flock64_t *bf)
  524. {
  525. struct inode *inode = file_inode(filp);
  526. struct xfs_inode *ip = XFS_I(inode);
  527. struct iattr iattr;
  528. enum xfs_prealloc_flags flags = 0;
  529. uint iolock = XFS_IOLOCK_EXCL;
  530. int error;
  531. /*
  532. * Only allow the sys admin to reserve space unless
  533. * unwritten extents are enabled.
  534. */
  535. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  536. !capable(CAP_SYS_ADMIN))
  537. return -EPERM;
  538. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  539. return -EPERM;
  540. if (!(filp->f_mode & FMODE_WRITE))
  541. return -EBADF;
  542. if (!S_ISREG(inode->i_mode))
  543. return -EINVAL;
  544. if (filp->f_flags & O_DSYNC)
  545. flags |= XFS_PREALLOC_SYNC;
  546. if (filp->f_mode & FMODE_NOCMTIME)
  547. flags |= XFS_PREALLOC_INVISIBLE;
  548. error = mnt_want_write_file(filp);
  549. if (error)
  550. return error;
  551. xfs_ilock(ip, iolock);
  552. error = xfs_break_layouts(inode, &iolock);
  553. if (error)
  554. goto out_unlock;
  555. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  556. iolock |= XFS_MMAPLOCK_EXCL;
  557. switch (bf->l_whence) {
  558. case 0: /*SEEK_SET*/
  559. break;
  560. case 1: /*SEEK_CUR*/
  561. bf->l_start += filp->f_pos;
  562. break;
  563. case 2: /*SEEK_END*/
  564. bf->l_start += XFS_ISIZE(ip);
  565. break;
  566. default:
  567. error = -EINVAL;
  568. goto out_unlock;
  569. }
  570. /*
  571. * length of <= 0 for resv/unresv/zero is invalid. length for
  572. * alloc/free is ignored completely and we have no idea what userspace
  573. * might have set it to, so set it to zero to allow range
  574. * checks to pass.
  575. */
  576. switch (cmd) {
  577. case XFS_IOC_ZERO_RANGE:
  578. case XFS_IOC_RESVSP:
  579. case XFS_IOC_RESVSP64:
  580. case XFS_IOC_UNRESVSP:
  581. case XFS_IOC_UNRESVSP64:
  582. if (bf->l_len <= 0) {
  583. error = -EINVAL;
  584. goto out_unlock;
  585. }
  586. break;
  587. default:
  588. bf->l_len = 0;
  589. break;
  590. }
  591. if (bf->l_start < 0 ||
  592. bf->l_start > inode->i_sb->s_maxbytes ||
  593. bf->l_start + bf->l_len < 0 ||
  594. bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
  595. error = -EINVAL;
  596. goto out_unlock;
  597. }
  598. switch (cmd) {
  599. case XFS_IOC_ZERO_RANGE:
  600. flags |= XFS_PREALLOC_SET;
  601. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  602. break;
  603. case XFS_IOC_RESVSP:
  604. case XFS_IOC_RESVSP64:
  605. flags |= XFS_PREALLOC_SET;
  606. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  607. XFS_BMAPI_PREALLOC);
  608. break;
  609. case XFS_IOC_UNRESVSP:
  610. case XFS_IOC_UNRESVSP64:
  611. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  612. break;
  613. case XFS_IOC_ALLOCSP:
  614. case XFS_IOC_ALLOCSP64:
  615. case XFS_IOC_FREESP:
  616. case XFS_IOC_FREESP64:
  617. flags |= XFS_PREALLOC_CLEAR;
  618. if (bf->l_start > XFS_ISIZE(ip)) {
  619. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  620. bf->l_start - XFS_ISIZE(ip), 0);
  621. if (error)
  622. goto out_unlock;
  623. }
  624. iattr.ia_valid = ATTR_SIZE;
  625. iattr.ia_size = bf->l_start;
  626. error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
  627. break;
  628. default:
  629. ASSERT(0);
  630. error = -EINVAL;
  631. }
  632. if (error)
  633. goto out_unlock;
  634. error = xfs_update_prealloc_flags(ip, flags);
  635. out_unlock:
  636. xfs_iunlock(ip, iolock);
  637. mnt_drop_write_file(filp);
  638. return error;
  639. }
  640. STATIC int
  641. xfs_ioc_bulkstat(
  642. xfs_mount_t *mp,
  643. unsigned int cmd,
  644. void __user *arg)
  645. {
  646. xfs_fsop_bulkreq_t bulkreq;
  647. int count; /* # of records returned */
  648. xfs_ino_t inlast; /* last inode number */
  649. int done;
  650. int error;
  651. /* done = 1 if there are more stats to get and if bulkstat */
  652. /* should be called again (unused here, but used in dmapi) */
  653. if (!capable(CAP_SYS_ADMIN))
  654. return -EPERM;
  655. if (XFS_FORCED_SHUTDOWN(mp))
  656. return -EIO;
  657. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  658. return -EFAULT;
  659. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  660. return -EFAULT;
  661. if ((count = bulkreq.icount) <= 0)
  662. return -EINVAL;
  663. if (bulkreq.ubuffer == NULL)
  664. return -EINVAL;
  665. if (cmd == XFS_IOC_FSINUMBERS)
  666. error = xfs_inumbers(mp, &inlast, &count,
  667. bulkreq.ubuffer, xfs_inumbers_fmt);
  668. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  669. error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
  670. sizeof(xfs_bstat_t), NULL, &done);
  671. else /* XFS_IOC_FSBULKSTAT */
  672. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  673. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  674. &done);
  675. if (error)
  676. return error;
  677. if (bulkreq.ocount != NULL) {
  678. if (copy_to_user(bulkreq.lastip, &inlast,
  679. sizeof(xfs_ino_t)))
  680. return -EFAULT;
  681. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  682. return -EFAULT;
  683. }
  684. return 0;
  685. }
  686. STATIC int
  687. xfs_ioc_fsgeometry_v1(
  688. xfs_mount_t *mp,
  689. void __user *arg)
  690. {
  691. xfs_fsop_geom_t fsgeo;
  692. int error;
  693. error = xfs_fs_geometry(mp, &fsgeo, 3);
  694. if (error)
  695. return error;
  696. /*
  697. * Caller should have passed an argument of type
  698. * xfs_fsop_geom_v1_t. This is a proper subset of the
  699. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  700. */
  701. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  702. return -EFAULT;
  703. return 0;
  704. }
  705. STATIC int
  706. xfs_ioc_fsgeometry(
  707. xfs_mount_t *mp,
  708. void __user *arg)
  709. {
  710. xfs_fsop_geom_t fsgeo;
  711. int error;
  712. error = xfs_fs_geometry(mp, &fsgeo, 4);
  713. if (error)
  714. return error;
  715. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  716. return -EFAULT;
  717. return 0;
  718. }
  719. /*
  720. * Linux extended inode flags interface.
  721. */
  722. STATIC unsigned int
  723. xfs_merge_ioc_xflags(
  724. unsigned int flags,
  725. unsigned int start)
  726. {
  727. unsigned int xflags = start;
  728. if (flags & FS_IMMUTABLE_FL)
  729. xflags |= FS_XFLAG_IMMUTABLE;
  730. else
  731. xflags &= ~FS_XFLAG_IMMUTABLE;
  732. if (flags & FS_APPEND_FL)
  733. xflags |= FS_XFLAG_APPEND;
  734. else
  735. xflags &= ~FS_XFLAG_APPEND;
  736. if (flags & FS_SYNC_FL)
  737. xflags |= FS_XFLAG_SYNC;
  738. else
  739. xflags &= ~FS_XFLAG_SYNC;
  740. if (flags & FS_NOATIME_FL)
  741. xflags |= FS_XFLAG_NOATIME;
  742. else
  743. xflags &= ~FS_XFLAG_NOATIME;
  744. if (flags & FS_NODUMP_FL)
  745. xflags |= FS_XFLAG_NODUMP;
  746. else
  747. xflags &= ~FS_XFLAG_NODUMP;
  748. return xflags;
  749. }
  750. STATIC unsigned int
  751. xfs_di2lxflags(
  752. uint16_t di_flags)
  753. {
  754. unsigned int flags = 0;
  755. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  756. flags |= FS_IMMUTABLE_FL;
  757. if (di_flags & XFS_DIFLAG_APPEND)
  758. flags |= FS_APPEND_FL;
  759. if (di_flags & XFS_DIFLAG_SYNC)
  760. flags |= FS_SYNC_FL;
  761. if (di_flags & XFS_DIFLAG_NOATIME)
  762. flags |= FS_NOATIME_FL;
  763. if (di_flags & XFS_DIFLAG_NODUMP)
  764. flags |= FS_NODUMP_FL;
  765. return flags;
  766. }
  767. STATIC int
  768. xfs_ioc_fsgetxattr(
  769. xfs_inode_t *ip,
  770. int attr,
  771. void __user *arg)
  772. {
  773. struct fsxattr fa;
  774. memset(&fa, 0, sizeof(struct fsxattr));
  775. xfs_ilock(ip, XFS_ILOCK_SHARED);
  776. fa.fsx_xflags = xfs_ip2xflags(ip);
  777. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  778. fa.fsx_cowextsize = ip->i_d.di_cowextsize <<
  779. ip->i_mount->m_sb.sb_blocklog;
  780. fa.fsx_projid = xfs_get_projid(ip);
  781. if (attr) {
  782. if (ip->i_afp) {
  783. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  784. fa.fsx_nextents = xfs_iext_count(ip->i_afp);
  785. else
  786. fa.fsx_nextents = ip->i_d.di_anextents;
  787. } else
  788. fa.fsx_nextents = 0;
  789. } else {
  790. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  791. fa.fsx_nextents = xfs_iext_count(&ip->i_df);
  792. else
  793. fa.fsx_nextents = ip->i_d.di_nextents;
  794. }
  795. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  796. if (copy_to_user(arg, &fa, sizeof(fa)))
  797. return -EFAULT;
  798. return 0;
  799. }
  800. STATIC void
  801. xfs_set_diflags(
  802. struct xfs_inode *ip,
  803. unsigned int xflags)
  804. {
  805. unsigned int di_flags;
  806. uint64_t di_flags2;
  807. /* can't set PREALLOC this way, just preserve it */
  808. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  809. if (xflags & FS_XFLAG_IMMUTABLE)
  810. di_flags |= XFS_DIFLAG_IMMUTABLE;
  811. if (xflags & FS_XFLAG_APPEND)
  812. di_flags |= XFS_DIFLAG_APPEND;
  813. if (xflags & FS_XFLAG_SYNC)
  814. di_flags |= XFS_DIFLAG_SYNC;
  815. if (xflags & FS_XFLAG_NOATIME)
  816. di_flags |= XFS_DIFLAG_NOATIME;
  817. if (xflags & FS_XFLAG_NODUMP)
  818. di_flags |= XFS_DIFLAG_NODUMP;
  819. if (xflags & FS_XFLAG_NODEFRAG)
  820. di_flags |= XFS_DIFLAG_NODEFRAG;
  821. if (xflags & FS_XFLAG_FILESTREAM)
  822. di_flags |= XFS_DIFLAG_FILESTREAM;
  823. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  824. if (xflags & FS_XFLAG_RTINHERIT)
  825. di_flags |= XFS_DIFLAG_RTINHERIT;
  826. if (xflags & FS_XFLAG_NOSYMLINKS)
  827. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  828. if (xflags & FS_XFLAG_EXTSZINHERIT)
  829. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  830. if (xflags & FS_XFLAG_PROJINHERIT)
  831. di_flags |= XFS_DIFLAG_PROJINHERIT;
  832. } else if (S_ISREG(VFS_I(ip)->i_mode)) {
  833. if (xflags & FS_XFLAG_REALTIME)
  834. di_flags |= XFS_DIFLAG_REALTIME;
  835. if (xflags & FS_XFLAG_EXTSIZE)
  836. di_flags |= XFS_DIFLAG_EXTSIZE;
  837. }
  838. ip->i_d.di_flags = di_flags;
  839. /* diflags2 only valid for v3 inodes. */
  840. if (ip->i_d.di_version < 3)
  841. return;
  842. di_flags2 = (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
  843. if (xflags & FS_XFLAG_DAX)
  844. di_flags2 |= XFS_DIFLAG2_DAX;
  845. if (xflags & FS_XFLAG_COWEXTSIZE)
  846. di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  847. ip->i_d.di_flags2 = di_flags2;
  848. }
  849. STATIC void
  850. xfs_diflags_to_linux(
  851. struct xfs_inode *ip)
  852. {
  853. struct inode *inode = VFS_I(ip);
  854. unsigned int xflags = xfs_ip2xflags(ip);
  855. if (xflags & FS_XFLAG_IMMUTABLE)
  856. inode->i_flags |= S_IMMUTABLE;
  857. else
  858. inode->i_flags &= ~S_IMMUTABLE;
  859. if (xflags & FS_XFLAG_APPEND)
  860. inode->i_flags |= S_APPEND;
  861. else
  862. inode->i_flags &= ~S_APPEND;
  863. if (xflags & FS_XFLAG_SYNC)
  864. inode->i_flags |= S_SYNC;
  865. else
  866. inode->i_flags &= ~S_SYNC;
  867. if (xflags & FS_XFLAG_NOATIME)
  868. inode->i_flags |= S_NOATIME;
  869. else
  870. inode->i_flags &= ~S_NOATIME;
  871. if (xflags & FS_XFLAG_DAX)
  872. inode->i_flags |= S_DAX;
  873. else
  874. inode->i_flags &= ~S_DAX;
  875. }
  876. static int
  877. xfs_ioctl_setattr_xflags(
  878. struct xfs_trans *tp,
  879. struct xfs_inode *ip,
  880. struct fsxattr *fa)
  881. {
  882. struct xfs_mount *mp = ip->i_mount;
  883. /* Can't change realtime flag if any extents are allocated. */
  884. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  885. XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
  886. return -EINVAL;
  887. /* If realtime flag is set then must have realtime device */
  888. if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
  889. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  890. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
  891. return -EINVAL;
  892. }
  893. /* Clear reflink if we are actually able to set the rt flag. */
  894. if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
  895. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  896. /* Don't allow us to set DAX mode for a reflinked file for now. */
  897. if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
  898. return -EINVAL;
  899. /*
  900. * Can't modify an immutable/append-only file unless
  901. * we have appropriate permission.
  902. */
  903. if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
  904. (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
  905. !capable(CAP_LINUX_IMMUTABLE))
  906. return -EPERM;
  907. xfs_set_diflags(ip, fa->fsx_xflags);
  908. xfs_diflags_to_linux(ip);
  909. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  910. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  911. XFS_STATS_INC(mp, xs_ig_attrchg);
  912. return 0;
  913. }
  914. /*
  915. * If we are changing DAX flags, we have to ensure the file is clean and any
  916. * cached objects in the address space are invalidated and removed. This
  917. * requires us to lock out other IO and page faults similar to a truncate
  918. * operation. The locks need to be held until the transaction has been committed
  919. * so that the cache invalidation is atomic with respect to the DAX flag
  920. * manipulation.
  921. */
  922. static int
  923. xfs_ioctl_setattr_dax_invalidate(
  924. struct xfs_inode *ip,
  925. struct fsxattr *fa,
  926. int *join_flags)
  927. {
  928. struct inode *inode = VFS_I(ip);
  929. int error;
  930. *join_flags = 0;
  931. /*
  932. * It is only valid to set the DAX flag on regular files and
  933. * directories on filesystems where the block size is equal to the page
  934. * size. On directories it serves as an inherit hint.
  935. */
  936. if (fa->fsx_xflags & FS_XFLAG_DAX) {
  937. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
  938. return -EINVAL;
  939. if (ip->i_mount->m_sb.sb_blocksize != PAGE_SIZE)
  940. return -EINVAL;
  941. }
  942. /* If the DAX state is not changing, we have nothing to do here. */
  943. if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
  944. return 0;
  945. if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
  946. return 0;
  947. /* lock, flush and invalidate mapping in preparation for flag change */
  948. xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  949. error = filemap_write_and_wait(inode->i_mapping);
  950. if (error)
  951. goto out_unlock;
  952. error = invalidate_inode_pages2(inode->i_mapping);
  953. if (error)
  954. goto out_unlock;
  955. *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
  956. return 0;
  957. out_unlock:
  958. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  959. return error;
  960. }
  961. /*
  962. * Set up the transaction structure for the setattr operation, checking that we
  963. * have permission to do so. On success, return a clean transaction and the
  964. * inode locked exclusively ready for further operation specific checks. On
  965. * failure, return an error without modifying or locking the inode.
  966. *
  967. * The inode might already be IO locked on call. If this is the case, it is
  968. * indicated in @join_flags and we take full responsibility for ensuring they
  969. * are unlocked from now on. Hence if we have an error here, we still have to
  970. * unlock them. Otherwise, once they are joined to the transaction, they will
  971. * be unlocked on commit/cancel.
  972. */
  973. static struct xfs_trans *
  974. xfs_ioctl_setattr_get_trans(
  975. struct xfs_inode *ip,
  976. int join_flags)
  977. {
  978. struct xfs_mount *mp = ip->i_mount;
  979. struct xfs_trans *tp;
  980. int error = -EROFS;
  981. if (mp->m_flags & XFS_MOUNT_RDONLY)
  982. goto out_unlock;
  983. error = -EIO;
  984. if (XFS_FORCED_SHUTDOWN(mp))
  985. goto out_unlock;
  986. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  987. if (error)
  988. return ERR_PTR(error);
  989. xfs_ilock(ip, XFS_ILOCK_EXCL);
  990. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
  991. join_flags = 0;
  992. /*
  993. * CAP_FOWNER overrides the following restrictions:
  994. *
  995. * The user ID of the calling process must be equal to the file owner
  996. * ID, except in cases where the CAP_FSETID capability is applicable.
  997. */
  998. if (!inode_owner_or_capable(VFS_I(ip))) {
  999. error = -EPERM;
  1000. goto out_cancel;
  1001. }
  1002. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1003. xfs_trans_set_sync(tp);
  1004. return tp;
  1005. out_cancel:
  1006. xfs_trans_cancel(tp);
  1007. out_unlock:
  1008. if (join_flags)
  1009. xfs_iunlock(ip, join_flags);
  1010. return ERR_PTR(error);
  1011. }
  1012. /*
  1013. * extent size hint validation is somewhat cumbersome. Rules are:
  1014. *
  1015. * 1. extent size hint is only valid for directories and regular files
  1016. * 2. FS_XFLAG_EXTSIZE is only valid for regular files
  1017. * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
  1018. * 4. can only be changed on regular files if no extents are allocated
  1019. * 5. can be changed on directories at any time
  1020. * 6. extsize hint of 0 turns off hints, clears inode flags.
  1021. * 7. Extent size must be a multiple of the appropriate block size.
  1022. * 8. for non-realtime files, the extent size hint must be limited
  1023. * to half the AG size to avoid alignment extending the extent beyond the
  1024. * limits of the AG.
  1025. */
  1026. static int
  1027. xfs_ioctl_setattr_check_extsize(
  1028. struct xfs_inode *ip,
  1029. struct fsxattr *fa)
  1030. {
  1031. struct xfs_mount *mp = ip->i_mount;
  1032. if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
  1033. return -EINVAL;
  1034. if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
  1035. !S_ISDIR(VFS_I(ip)->i_mode))
  1036. return -EINVAL;
  1037. if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
  1038. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
  1039. return -EINVAL;
  1040. if (fa->fsx_extsize != 0) {
  1041. xfs_extlen_t size;
  1042. xfs_fsblock_t extsize_fsb;
  1043. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  1044. if (extsize_fsb > MAXEXTLEN)
  1045. return -EINVAL;
  1046. if (XFS_IS_REALTIME_INODE(ip) ||
  1047. (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
  1048. size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
  1049. } else {
  1050. size = mp->m_sb.sb_blocksize;
  1051. if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
  1052. return -EINVAL;
  1053. }
  1054. if (fa->fsx_extsize % size)
  1055. return -EINVAL;
  1056. } else
  1057. fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
  1058. return 0;
  1059. }
  1060. /*
  1061. * CoW extent size hint validation rules are:
  1062. *
  1063. * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
  1064. * The inode does not have to have any shared blocks, but it must be a v3.
  1065. * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
  1066. * for a directory, the hint is propagated to new files.
  1067. * 3. Can be changed on files & directories at any time.
  1068. * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
  1069. * 5. Extent size must be a multiple of the appropriate block size.
  1070. * 6. The extent size hint must be limited to half the AG size to avoid
  1071. * alignment extending the extent beyond the limits of the AG.
  1072. */
  1073. static int
  1074. xfs_ioctl_setattr_check_cowextsize(
  1075. struct xfs_inode *ip,
  1076. struct fsxattr *fa)
  1077. {
  1078. struct xfs_mount *mp = ip->i_mount;
  1079. if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
  1080. return 0;
  1081. if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
  1082. ip->i_d.di_version != 3)
  1083. return -EINVAL;
  1084. if (!S_ISREG(VFS_I(ip)->i_mode) && !S_ISDIR(VFS_I(ip)->i_mode))
  1085. return -EINVAL;
  1086. if (fa->fsx_cowextsize != 0) {
  1087. xfs_extlen_t size;
  1088. xfs_fsblock_t cowextsize_fsb;
  1089. cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
  1090. if (cowextsize_fsb > MAXEXTLEN)
  1091. return -EINVAL;
  1092. size = mp->m_sb.sb_blocksize;
  1093. if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
  1094. return -EINVAL;
  1095. if (fa->fsx_cowextsize % size)
  1096. return -EINVAL;
  1097. } else
  1098. fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
  1099. return 0;
  1100. }
  1101. static int
  1102. xfs_ioctl_setattr_check_projid(
  1103. struct xfs_inode *ip,
  1104. struct fsxattr *fa)
  1105. {
  1106. /* Disallow 32bit project ids if projid32bit feature is not enabled. */
  1107. if (fa->fsx_projid > (uint16_t)-1 &&
  1108. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  1109. return -EINVAL;
  1110. /*
  1111. * Project Quota ID state is only allowed to change from within the init
  1112. * namespace. Enforce that restriction only if we are trying to change
  1113. * the quota ID state. Everything else is allowed in user namespaces.
  1114. */
  1115. if (current_user_ns() == &init_user_ns)
  1116. return 0;
  1117. if (xfs_get_projid(ip) != fa->fsx_projid)
  1118. return -EINVAL;
  1119. if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
  1120. (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  1121. return -EINVAL;
  1122. return 0;
  1123. }
  1124. STATIC int
  1125. xfs_ioctl_setattr(
  1126. xfs_inode_t *ip,
  1127. struct fsxattr *fa)
  1128. {
  1129. struct xfs_mount *mp = ip->i_mount;
  1130. struct xfs_trans *tp;
  1131. struct xfs_dquot *udqp = NULL;
  1132. struct xfs_dquot *pdqp = NULL;
  1133. struct xfs_dquot *olddquot = NULL;
  1134. int code;
  1135. int join_flags = 0;
  1136. trace_xfs_ioctl_setattr(ip);
  1137. code = xfs_ioctl_setattr_check_projid(ip, fa);
  1138. if (code)
  1139. return code;
  1140. /*
  1141. * If disk quotas is on, we make sure that the dquots do exist on disk,
  1142. * before we start any other transactions. Trying to do this later
  1143. * is messy. We don't care to take a readlock to look at the ids
  1144. * in inode here, because we can't hold it across the trans_reserve.
  1145. * If the IDs do change before we take the ilock, we're covered
  1146. * because the i_*dquot fields will get updated anyway.
  1147. */
  1148. if (XFS_IS_QUOTA_ON(mp)) {
  1149. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  1150. ip->i_d.di_gid, fa->fsx_projid,
  1151. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  1152. if (code)
  1153. return code;
  1154. }
  1155. /*
  1156. * Changing DAX config may require inode locking for mapping
  1157. * invalidation. These need to be held all the way to transaction commit
  1158. * or cancel time, so need to be passed through to
  1159. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1160. * appropriately.
  1161. */
  1162. code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
  1163. if (code)
  1164. goto error_free_dquots;
  1165. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1166. if (IS_ERR(tp)) {
  1167. code = PTR_ERR(tp);
  1168. goto error_free_dquots;
  1169. }
  1170. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
  1171. xfs_get_projid(ip) != fa->fsx_projid) {
  1172. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
  1173. capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
  1174. if (code) /* out of quota */
  1175. goto error_trans_cancel;
  1176. }
  1177. code = xfs_ioctl_setattr_check_extsize(ip, fa);
  1178. if (code)
  1179. goto error_trans_cancel;
  1180. code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
  1181. if (code)
  1182. goto error_trans_cancel;
  1183. code = xfs_ioctl_setattr_xflags(tp, ip, fa);
  1184. if (code)
  1185. goto error_trans_cancel;
  1186. /*
  1187. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  1188. * overrides the following restrictions:
  1189. *
  1190. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  1191. * successful return from chown()
  1192. */
  1193. if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
  1194. !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
  1195. VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
  1196. /* Change the ownerships and register project quota modifications */
  1197. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1198. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1199. olddquot = xfs_qm_vop_chown(tp, ip,
  1200. &ip->i_pdquot, pdqp);
  1201. }
  1202. ASSERT(ip->i_d.di_version > 1);
  1203. xfs_set_projid(ip, fa->fsx_projid);
  1204. }
  1205. /*
  1206. * Only set the extent size hint if we've already determined that the
  1207. * extent size hint should be set on the inode. If no extent size flags
  1208. * are set on the inode then unconditionally clear the extent size hint.
  1209. */
  1210. if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  1211. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1212. else
  1213. ip->i_d.di_extsize = 0;
  1214. if (ip->i_d.di_version == 3 &&
  1215. (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1216. ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
  1217. mp->m_sb.sb_blocklog;
  1218. else
  1219. ip->i_d.di_cowextsize = 0;
  1220. code = xfs_trans_commit(tp);
  1221. /*
  1222. * Release any dquot(s) the inode had kept before chown.
  1223. */
  1224. xfs_qm_dqrele(olddquot);
  1225. xfs_qm_dqrele(udqp);
  1226. xfs_qm_dqrele(pdqp);
  1227. return code;
  1228. error_trans_cancel:
  1229. xfs_trans_cancel(tp);
  1230. error_free_dquots:
  1231. xfs_qm_dqrele(udqp);
  1232. xfs_qm_dqrele(pdqp);
  1233. return code;
  1234. }
  1235. STATIC int
  1236. xfs_ioc_fssetxattr(
  1237. xfs_inode_t *ip,
  1238. struct file *filp,
  1239. void __user *arg)
  1240. {
  1241. struct fsxattr fa;
  1242. int error;
  1243. if (copy_from_user(&fa, arg, sizeof(fa)))
  1244. return -EFAULT;
  1245. error = mnt_want_write_file(filp);
  1246. if (error)
  1247. return error;
  1248. error = xfs_ioctl_setattr(ip, &fa);
  1249. mnt_drop_write_file(filp);
  1250. return error;
  1251. }
  1252. STATIC int
  1253. xfs_ioc_getxflags(
  1254. xfs_inode_t *ip,
  1255. void __user *arg)
  1256. {
  1257. unsigned int flags;
  1258. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1259. if (copy_to_user(arg, &flags, sizeof(flags)))
  1260. return -EFAULT;
  1261. return 0;
  1262. }
  1263. STATIC int
  1264. xfs_ioc_setxflags(
  1265. struct xfs_inode *ip,
  1266. struct file *filp,
  1267. void __user *arg)
  1268. {
  1269. struct xfs_trans *tp;
  1270. struct fsxattr fa;
  1271. unsigned int flags;
  1272. int join_flags = 0;
  1273. int error;
  1274. if (copy_from_user(&flags, arg, sizeof(flags)))
  1275. return -EFAULT;
  1276. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1277. FS_NOATIME_FL | FS_NODUMP_FL | \
  1278. FS_SYNC_FL))
  1279. return -EOPNOTSUPP;
  1280. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1281. error = mnt_want_write_file(filp);
  1282. if (error)
  1283. return error;
  1284. /*
  1285. * Changing DAX config may require inode locking for mapping
  1286. * invalidation. These need to be held all the way to transaction commit
  1287. * or cancel time, so need to be passed through to
  1288. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1289. * appropriately.
  1290. */
  1291. error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
  1292. if (error)
  1293. goto out_drop_write;
  1294. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1295. if (IS_ERR(tp)) {
  1296. error = PTR_ERR(tp);
  1297. goto out_drop_write;
  1298. }
  1299. error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
  1300. if (error) {
  1301. xfs_trans_cancel(tp);
  1302. goto out_drop_write;
  1303. }
  1304. error = xfs_trans_commit(tp);
  1305. out_drop_write:
  1306. mnt_drop_write_file(filp);
  1307. return error;
  1308. }
  1309. STATIC int
  1310. xfs_getbmap_format(void **ap, struct getbmapx *bmv)
  1311. {
  1312. struct getbmap __user *base = (struct getbmap __user *)*ap;
  1313. /* copy only getbmap portion (not getbmapx) */
  1314. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1315. return -EFAULT;
  1316. *ap += sizeof(struct getbmap);
  1317. return 0;
  1318. }
  1319. STATIC int
  1320. xfs_ioc_getbmap(
  1321. struct file *file,
  1322. unsigned int cmd,
  1323. void __user *arg)
  1324. {
  1325. struct getbmapx bmx = { 0 };
  1326. int error;
  1327. /* struct getbmap is a strict subset of struct getbmapx. */
  1328. if (copy_from_user(&bmx, arg, offsetof(struct getbmapx, bmv_iflags)))
  1329. return -EFAULT;
  1330. if (bmx.bmv_count < 2)
  1331. return -EINVAL;
  1332. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1333. if (file->f_mode & FMODE_NOCMTIME)
  1334. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1335. error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, xfs_getbmap_format,
  1336. (__force struct getbmap *)arg+1);
  1337. if (error)
  1338. return error;
  1339. /* copy back header - only size of getbmap */
  1340. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1341. return -EFAULT;
  1342. return 0;
  1343. }
  1344. STATIC int
  1345. xfs_getbmapx_format(void **ap, struct getbmapx *bmv)
  1346. {
  1347. struct getbmapx __user *base = (struct getbmapx __user *)*ap;
  1348. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1349. return -EFAULT;
  1350. *ap += sizeof(struct getbmapx);
  1351. return 0;
  1352. }
  1353. STATIC int
  1354. xfs_ioc_getbmapx(
  1355. struct xfs_inode *ip,
  1356. void __user *arg)
  1357. {
  1358. struct getbmapx bmx;
  1359. int error;
  1360. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1361. return -EFAULT;
  1362. if (bmx.bmv_count < 2)
  1363. return -EINVAL;
  1364. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1365. return -EINVAL;
  1366. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1367. (__force struct getbmapx *)arg+1);
  1368. if (error)
  1369. return error;
  1370. /* copy back header */
  1371. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1372. return -EFAULT;
  1373. return 0;
  1374. }
  1375. struct getfsmap_info {
  1376. struct xfs_mount *mp;
  1377. struct fsmap_head __user *data;
  1378. unsigned int idx;
  1379. __u32 last_flags;
  1380. };
  1381. STATIC int
  1382. xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
  1383. {
  1384. struct getfsmap_info *info = priv;
  1385. struct fsmap fm;
  1386. trace_xfs_getfsmap_mapping(info->mp, xfm);
  1387. info->last_flags = xfm->fmr_flags;
  1388. xfs_fsmap_from_internal(&fm, xfm);
  1389. if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
  1390. sizeof(struct fsmap)))
  1391. return -EFAULT;
  1392. return 0;
  1393. }
  1394. STATIC int
  1395. xfs_ioc_getfsmap(
  1396. struct xfs_inode *ip,
  1397. struct fsmap_head __user *arg)
  1398. {
  1399. struct getfsmap_info info = { NULL };
  1400. struct xfs_fsmap_head xhead = {0};
  1401. struct fsmap_head head;
  1402. bool aborted = false;
  1403. int error;
  1404. if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
  1405. return -EFAULT;
  1406. if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
  1407. memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
  1408. sizeof(head.fmh_keys[0].fmr_reserved)) ||
  1409. memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
  1410. sizeof(head.fmh_keys[1].fmr_reserved)))
  1411. return -EINVAL;
  1412. xhead.fmh_iflags = head.fmh_iflags;
  1413. xhead.fmh_count = head.fmh_count;
  1414. xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
  1415. xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
  1416. trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
  1417. trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
  1418. info.mp = ip->i_mount;
  1419. info.data = arg;
  1420. error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
  1421. if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
  1422. error = 0;
  1423. aborted = true;
  1424. } else if (error)
  1425. return error;
  1426. /* If we didn't abort, set the "last" flag in the last fmx */
  1427. if (!aborted && info.idx) {
  1428. info.last_flags |= FMR_OF_LAST;
  1429. if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
  1430. &info.last_flags, sizeof(info.last_flags)))
  1431. return -EFAULT;
  1432. }
  1433. /* copy back header */
  1434. head.fmh_entries = xhead.fmh_entries;
  1435. head.fmh_oflags = xhead.fmh_oflags;
  1436. if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
  1437. return -EFAULT;
  1438. return 0;
  1439. }
  1440. int
  1441. xfs_ioc_swapext(
  1442. xfs_swapext_t *sxp)
  1443. {
  1444. xfs_inode_t *ip, *tip;
  1445. struct fd f, tmp;
  1446. int error = 0;
  1447. /* Pull information for the target fd */
  1448. f = fdget((int)sxp->sx_fdtarget);
  1449. if (!f.file) {
  1450. error = -EINVAL;
  1451. goto out;
  1452. }
  1453. if (!(f.file->f_mode & FMODE_WRITE) ||
  1454. !(f.file->f_mode & FMODE_READ) ||
  1455. (f.file->f_flags & O_APPEND)) {
  1456. error = -EBADF;
  1457. goto out_put_file;
  1458. }
  1459. tmp = fdget((int)sxp->sx_fdtmp);
  1460. if (!tmp.file) {
  1461. error = -EINVAL;
  1462. goto out_put_file;
  1463. }
  1464. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1465. !(tmp.file->f_mode & FMODE_READ) ||
  1466. (tmp.file->f_flags & O_APPEND)) {
  1467. error = -EBADF;
  1468. goto out_put_tmp_file;
  1469. }
  1470. if (IS_SWAPFILE(file_inode(f.file)) ||
  1471. IS_SWAPFILE(file_inode(tmp.file))) {
  1472. error = -EINVAL;
  1473. goto out_put_tmp_file;
  1474. }
  1475. /*
  1476. * We need to ensure that the fds passed in point to XFS inodes
  1477. * before we cast and access them as XFS structures as we have no
  1478. * control over what the user passes us here.
  1479. */
  1480. if (f.file->f_op != &xfs_file_operations ||
  1481. tmp.file->f_op != &xfs_file_operations) {
  1482. error = -EINVAL;
  1483. goto out_put_tmp_file;
  1484. }
  1485. ip = XFS_I(file_inode(f.file));
  1486. tip = XFS_I(file_inode(tmp.file));
  1487. if (ip->i_mount != tip->i_mount) {
  1488. error = -EINVAL;
  1489. goto out_put_tmp_file;
  1490. }
  1491. if (ip->i_ino == tip->i_ino) {
  1492. error = -EINVAL;
  1493. goto out_put_tmp_file;
  1494. }
  1495. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1496. error = -EIO;
  1497. goto out_put_tmp_file;
  1498. }
  1499. error = xfs_swap_extents(ip, tip, sxp);
  1500. out_put_tmp_file:
  1501. fdput(tmp);
  1502. out_put_file:
  1503. fdput(f);
  1504. out:
  1505. return error;
  1506. }
  1507. /*
  1508. * Note: some of the ioctl's return positive numbers as a
  1509. * byte count indicating success, such as readlink_by_handle.
  1510. * So we don't "sign flip" like most other routines. This means
  1511. * true errors need to be returned as a negative value.
  1512. */
  1513. long
  1514. xfs_file_ioctl(
  1515. struct file *filp,
  1516. unsigned int cmd,
  1517. unsigned long p)
  1518. {
  1519. struct inode *inode = file_inode(filp);
  1520. struct xfs_inode *ip = XFS_I(inode);
  1521. struct xfs_mount *mp = ip->i_mount;
  1522. void __user *arg = (void __user *)p;
  1523. int error;
  1524. trace_xfs_file_ioctl(ip);
  1525. switch (cmd) {
  1526. case FITRIM:
  1527. return xfs_ioc_trim(mp, arg);
  1528. case XFS_IOC_ALLOCSP:
  1529. case XFS_IOC_FREESP:
  1530. case XFS_IOC_RESVSP:
  1531. case XFS_IOC_UNRESVSP:
  1532. case XFS_IOC_ALLOCSP64:
  1533. case XFS_IOC_FREESP64:
  1534. case XFS_IOC_RESVSP64:
  1535. case XFS_IOC_UNRESVSP64:
  1536. case XFS_IOC_ZERO_RANGE: {
  1537. xfs_flock64_t bf;
  1538. if (copy_from_user(&bf, arg, sizeof(bf)))
  1539. return -EFAULT;
  1540. return xfs_ioc_space(filp, cmd, &bf);
  1541. }
  1542. case XFS_IOC_DIOINFO: {
  1543. struct dioattr da;
  1544. xfs_buftarg_t *target =
  1545. XFS_IS_REALTIME_INODE(ip) ?
  1546. mp->m_rtdev_targp : mp->m_ddev_targp;
  1547. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1548. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1549. if (copy_to_user(arg, &da, sizeof(da)))
  1550. return -EFAULT;
  1551. return 0;
  1552. }
  1553. case XFS_IOC_FSBULKSTAT_SINGLE:
  1554. case XFS_IOC_FSBULKSTAT:
  1555. case XFS_IOC_FSINUMBERS:
  1556. return xfs_ioc_bulkstat(mp, cmd, arg);
  1557. case XFS_IOC_FSGEOMETRY_V1:
  1558. return xfs_ioc_fsgeometry_v1(mp, arg);
  1559. case XFS_IOC_FSGEOMETRY:
  1560. return xfs_ioc_fsgeometry(mp, arg);
  1561. case XFS_IOC_GETVERSION:
  1562. return put_user(inode->i_generation, (int __user *)arg);
  1563. case XFS_IOC_FSGETXATTR:
  1564. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1565. case XFS_IOC_FSGETXATTRA:
  1566. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1567. case XFS_IOC_FSSETXATTR:
  1568. return xfs_ioc_fssetxattr(ip, filp, arg);
  1569. case XFS_IOC_GETXFLAGS:
  1570. return xfs_ioc_getxflags(ip, arg);
  1571. case XFS_IOC_SETXFLAGS:
  1572. return xfs_ioc_setxflags(ip, filp, arg);
  1573. case XFS_IOC_FSSETDM: {
  1574. struct fsdmidata dmi;
  1575. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1576. return -EFAULT;
  1577. error = mnt_want_write_file(filp);
  1578. if (error)
  1579. return error;
  1580. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1581. dmi.fsd_dmstate);
  1582. mnt_drop_write_file(filp);
  1583. return error;
  1584. }
  1585. case XFS_IOC_GETBMAP:
  1586. case XFS_IOC_GETBMAPA:
  1587. return xfs_ioc_getbmap(filp, cmd, arg);
  1588. case XFS_IOC_GETBMAPX:
  1589. return xfs_ioc_getbmapx(ip, arg);
  1590. case FS_IOC_GETFSMAP:
  1591. return xfs_ioc_getfsmap(ip, arg);
  1592. case XFS_IOC_FD_TO_HANDLE:
  1593. case XFS_IOC_PATH_TO_HANDLE:
  1594. case XFS_IOC_PATH_TO_FSHANDLE: {
  1595. xfs_fsop_handlereq_t hreq;
  1596. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1597. return -EFAULT;
  1598. return xfs_find_handle(cmd, &hreq);
  1599. }
  1600. case XFS_IOC_OPEN_BY_HANDLE: {
  1601. xfs_fsop_handlereq_t hreq;
  1602. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1603. return -EFAULT;
  1604. return xfs_open_by_handle(filp, &hreq);
  1605. }
  1606. case XFS_IOC_FSSETDM_BY_HANDLE:
  1607. return xfs_fssetdm_by_handle(filp, arg);
  1608. case XFS_IOC_READLINK_BY_HANDLE: {
  1609. xfs_fsop_handlereq_t hreq;
  1610. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1611. return -EFAULT;
  1612. return xfs_readlink_by_handle(filp, &hreq);
  1613. }
  1614. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1615. return xfs_attrlist_by_handle(filp, arg);
  1616. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1617. return xfs_attrmulti_by_handle(filp, arg);
  1618. case XFS_IOC_SWAPEXT: {
  1619. struct xfs_swapext sxp;
  1620. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1621. return -EFAULT;
  1622. error = mnt_want_write_file(filp);
  1623. if (error)
  1624. return error;
  1625. error = xfs_ioc_swapext(&sxp);
  1626. mnt_drop_write_file(filp);
  1627. return error;
  1628. }
  1629. case XFS_IOC_FSCOUNTS: {
  1630. xfs_fsop_counts_t out;
  1631. error = xfs_fs_counts(mp, &out);
  1632. if (error)
  1633. return error;
  1634. if (copy_to_user(arg, &out, sizeof(out)))
  1635. return -EFAULT;
  1636. return 0;
  1637. }
  1638. case XFS_IOC_SET_RESBLKS: {
  1639. xfs_fsop_resblks_t inout;
  1640. uint64_t in;
  1641. if (!capable(CAP_SYS_ADMIN))
  1642. return -EPERM;
  1643. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1644. return -EROFS;
  1645. if (copy_from_user(&inout, arg, sizeof(inout)))
  1646. return -EFAULT;
  1647. error = mnt_want_write_file(filp);
  1648. if (error)
  1649. return error;
  1650. /* input parameter is passed in resblks field of structure */
  1651. in = inout.resblks;
  1652. error = xfs_reserve_blocks(mp, &in, &inout);
  1653. mnt_drop_write_file(filp);
  1654. if (error)
  1655. return error;
  1656. if (copy_to_user(arg, &inout, sizeof(inout)))
  1657. return -EFAULT;
  1658. return 0;
  1659. }
  1660. case XFS_IOC_GET_RESBLKS: {
  1661. xfs_fsop_resblks_t out;
  1662. if (!capable(CAP_SYS_ADMIN))
  1663. return -EPERM;
  1664. error = xfs_reserve_blocks(mp, NULL, &out);
  1665. if (error)
  1666. return error;
  1667. if (copy_to_user(arg, &out, sizeof(out)))
  1668. return -EFAULT;
  1669. return 0;
  1670. }
  1671. case XFS_IOC_FSGROWFSDATA: {
  1672. xfs_growfs_data_t in;
  1673. if (copy_from_user(&in, arg, sizeof(in)))
  1674. return -EFAULT;
  1675. error = mnt_want_write_file(filp);
  1676. if (error)
  1677. return error;
  1678. error = xfs_growfs_data(mp, &in);
  1679. mnt_drop_write_file(filp);
  1680. return error;
  1681. }
  1682. case XFS_IOC_FSGROWFSLOG: {
  1683. xfs_growfs_log_t in;
  1684. if (copy_from_user(&in, arg, sizeof(in)))
  1685. return -EFAULT;
  1686. error = mnt_want_write_file(filp);
  1687. if (error)
  1688. return error;
  1689. error = xfs_growfs_log(mp, &in);
  1690. mnt_drop_write_file(filp);
  1691. return error;
  1692. }
  1693. case XFS_IOC_FSGROWFSRT: {
  1694. xfs_growfs_rt_t in;
  1695. if (copy_from_user(&in, arg, sizeof(in)))
  1696. return -EFAULT;
  1697. error = mnt_want_write_file(filp);
  1698. if (error)
  1699. return error;
  1700. error = xfs_growfs_rt(mp, &in);
  1701. mnt_drop_write_file(filp);
  1702. return error;
  1703. }
  1704. case XFS_IOC_GOINGDOWN: {
  1705. uint32_t in;
  1706. if (!capable(CAP_SYS_ADMIN))
  1707. return -EPERM;
  1708. if (get_user(in, (uint32_t __user *)arg))
  1709. return -EFAULT;
  1710. return xfs_fs_goingdown(mp, in);
  1711. }
  1712. case XFS_IOC_ERROR_INJECTION: {
  1713. xfs_error_injection_t in;
  1714. if (!capable(CAP_SYS_ADMIN))
  1715. return -EPERM;
  1716. if (copy_from_user(&in, arg, sizeof(in)))
  1717. return -EFAULT;
  1718. return xfs_errortag_add(mp, in.errtag);
  1719. }
  1720. case XFS_IOC_ERROR_CLEARALL:
  1721. if (!capable(CAP_SYS_ADMIN))
  1722. return -EPERM;
  1723. return xfs_errortag_clearall(mp);
  1724. case XFS_IOC_FREE_EOFBLOCKS: {
  1725. struct xfs_fs_eofblocks eofb;
  1726. struct xfs_eofblocks keofb;
  1727. if (!capable(CAP_SYS_ADMIN))
  1728. return -EPERM;
  1729. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1730. return -EROFS;
  1731. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1732. return -EFAULT;
  1733. error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
  1734. if (error)
  1735. return error;
  1736. return xfs_icache_free_eofblocks(mp, &keofb);
  1737. }
  1738. default:
  1739. return -ENOTTY;
  1740. }
  1741. }