xfs_ioctl.c 39 KB

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