xfs_ioctl.c 40 KB

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