xfs_ioctl.c 38 KB

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