xfs_ioctl.c 50 KB

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