ioctl.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/highmem.h>
  26. #include <linux/time.h>
  27. #include <linux/init.h>
  28. #include <linux/string.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mount.h>
  31. #include <linux/mpage.h>
  32. #include <linux/namei.h>
  33. #include <linux/swap.h>
  34. #include <linux/writeback.h>
  35. #include <linux/statfs.h>
  36. #include <linux/compat.h>
  37. #include <linux/bit_spinlock.h>
  38. #include <linux/security.h>
  39. #include <linux/xattr.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/slab.h>
  42. #include "compat.h"
  43. #include "ctree.h"
  44. #include "disk-io.h"
  45. #include "transaction.h"
  46. #include "btrfs_inode.h"
  47. #include "ioctl.h"
  48. #include "print-tree.h"
  49. #include "volumes.h"
  50. #include "locking.h"
  51. /* Mask out flags that are inappropriate for the given type of inode. */
  52. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  53. {
  54. if (S_ISDIR(mode))
  55. return flags;
  56. else if (S_ISREG(mode))
  57. return flags & ~FS_DIRSYNC_FL;
  58. else
  59. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  60. }
  61. /*
  62. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  63. */
  64. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  65. {
  66. unsigned int iflags = 0;
  67. if (flags & BTRFS_INODE_SYNC)
  68. iflags |= FS_SYNC_FL;
  69. if (flags & BTRFS_INODE_IMMUTABLE)
  70. iflags |= FS_IMMUTABLE_FL;
  71. if (flags & BTRFS_INODE_APPEND)
  72. iflags |= FS_APPEND_FL;
  73. if (flags & BTRFS_INODE_NODUMP)
  74. iflags |= FS_NODUMP_FL;
  75. if (flags & BTRFS_INODE_NOATIME)
  76. iflags |= FS_NOATIME_FL;
  77. if (flags & BTRFS_INODE_DIRSYNC)
  78. iflags |= FS_DIRSYNC_FL;
  79. return iflags;
  80. }
  81. /*
  82. * Update inode->i_flags based on the btrfs internal flags.
  83. */
  84. void btrfs_update_iflags(struct inode *inode)
  85. {
  86. struct btrfs_inode *ip = BTRFS_I(inode);
  87. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  88. if (ip->flags & BTRFS_INODE_SYNC)
  89. inode->i_flags |= S_SYNC;
  90. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  91. inode->i_flags |= S_IMMUTABLE;
  92. if (ip->flags & BTRFS_INODE_APPEND)
  93. inode->i_flags |= S_APPEND;
  94. if (ip->flags & BTRFS_INODE_NOATIME)
  95. inode->i_flags |= S_NOATIME;
  96. if (ip->flags & BTRFS_INODE_DIRSYNC)
  97. inode->i_flags |= S_DIRSYNC;
  98. }
  99. /*
  100. * Inherit flags from the parent inode.
  101. *
  102. * Unlike extN we don't have any flags we don't want to inherit currently.
  103. */
  104. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  105. {
  106. unsigned int flags;
  107. if (!dir)
  108. return;
  109. flags = BTRFS_I(dir)->flags;
  110. if (S_ISREG(inode->i_mode))
  111. flags &= ~BTRFS_INODE_DIRSYNC;
  112. else if (!S_ISDIR(inode->i_mode))
  113. flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
  114. BTRFS_I(inode)->flags = flags;
  115. btrfs_update_iflags(inode);
  116. }
  117. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  118. {
  119. struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
  120. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  121. if (copy_to_user(arg, &flags, sizeof(flags)))
  122. return -EFAULT;
  123. return 0;
  124. }
  125. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  126. {
  127. struct inode *inode = file->f_path.dentry->d_inode;
  128. struct btrfs_inode *ip = BTRFS_I(inode);
  129. struct btrfs_root *root = ip->root;
  130. struct btrfs_trans_handle *trans;
  131. unsigned int flags, oldflags;
  132. int ret;
  133. if (btrfs_root_readonly(root))
  134. return -EROFS;
  135. if (copy_from_user(&flags, arg, sizeof(flags)))
  136. return -EFAULT;
  137. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  138. FS_NOATIME_FL | FS_NODUMP_FL | \
  139. FS_SYNC_FL | FS_DIRSYNC_FL))
  140. return -EOPNOTSUPP;
  141. if (!is_owner_or_cap(inode))
  142. return -EACCES;
  143. mutex_lock(&inode->i_mutex);
  144. flags = btrfs_mask_flags(inode->i_mode, flags);
  145. oldflags = btrfs_flags_to_ioctl(ip->flags);
  146. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  147. if (!capable(CAP_LINUX_IMMUTABLE)) {
  148. ret = -EPERM;
  149. goto out_unlock;
  150. }
  151. }
  152. ret = mnt_want_write(file->f_path.mnt);
  153. if (ret)
  154. goto out_unlock;
  155. if (flags & FS_SYNC_FL)
  156. ip->flags |= BTRFS_INODE_SYNC;
  157. else
  158. ip->flags &= ~BTRFS_INODE_SYNC;
  159. if (flags & FS_IMMUTABLE_FL)
  160. ip->flags |= BTRFS_INODE_IMMUTABLE;
  161. else
  162. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  163. if (flags & FS_APPEND_FL)
  164. ip->flags |= BTRFS_INODE_APPEND;
  165. else
  166. ip->flags &= ~BTRFS_INODE_APPEND;
  167. if (flags & FS_NODUMP_FL)
  168. ip->flags |= BTRFS_INODE_NODUMP;
  169. else
  170. ip->flags &= ~BTRFS_INODE_NODUMP;
  171. if (flags & FS_NOATIME_FL)
  172. ip->flags |= BTRFS_INODE_NOATIME;
  173. else
  174. ip->flags &= ~BTRFS_INODE_NOATIME;
  175. if (flags & FS_DIRSYNC_FL)
  176. ip->flags |= BTRFS_INODE_DIRSYNC;
  177. else
  178. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  179. trans = btrfs_join_transaction(root, 1);
  180. BUG_ON(IS_ERR(trans));
  181. ret = btrfs_update_inode(trans, root, inode);
  182. BUG_ON(ret);
  183. btrfs_update_iflags(inode);
  184. inode->i_ctime = CURRENT_TIME;
  185. btrfs_end_transaction(trans, root);
  186. mnt_drop_write(file->f_path.mnt);
  187. out_unlock:
  188. mutex_unlock(&inode->i_mutex);
  189. return 0;
  190. }
  191. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  192. {
  193. struct inode *inode = file->f_path.dentry->d_inode;
  194. return put_user(inode->i_generation, arg);
  195. }
  196. static noinline int create_subvol(struct btrfs_root *root,
  197. struct dentry *dentry,
  198. char *name, int namelen,
  199. u64 *async_transid)
  200. {
  201. struct btrfs_trans_handle *trans;
  202. struct btrfs_key key;
  203. struct btrfs_root_item root_item;
  204. struct btrfs_inode_item *inode_item;
  205. struct extent_buffer *leaf;
  206. struct btrfs_root *new_root;
  207. struct dentry *parent = dget_parent(dentry);
  208. struct inode *dir;
  209. int ret;
  210. int err;
  211. u64 objectid;
  212. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  213. u64 index = 0;
  214. ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
  215. 0, &objectid);
  216. if (ret) {
  217. dput(parent);
  218. return ret;
  219. }
  220. dir = parent->d_inode;
  221. /*
  222. * 1 - inode item
  223. * 2 - refs
  224. * 1 - root item
  225. * 2 - dir items
  226. */
  227. trans = btrfs_start_transaction(root, 6);
  228. if (IS_ERR(trans)) {
  229. dput(parent);
  230. return PTR_ERR(trans);
  231. }
  232. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  233. 0, objectid, NULL, 0, 0, 0);
  234. if (IS_ERR(leaf)) {
  235. ret = PTR_ERR(leaf);
  236. goto fail;
  237. }
  238. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  239. btrfs_set_header_bytenr(leaf, leaf->start);
  240. btrfs_set_header_generation(leaf, trans->transid);
  241. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  242. btrfs_set_header_owner(leaf, objectid);
  243. write_extent_buffer(leaf, root->fs_info->fsid,
  244. (unsigned long)btrfs_header_fsid(leaf),
  245. BTRFS_FSID_SIZE);
  246. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  247. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  248. BTRFS_UUID_SIZE);
  249. btrfs_mark_buffer_dirty(leaf);
  250. inode_item = &root_item.inode;
  251. memset(inode_item, 0, sizeof(*inode_item));
  252. inode_item->generation = cpu_to_le64(1);
  253. inode_item->size = cpu_to_le64(3);
  254. inode_item->nlink = cpu_to_le32(1);
  255. inode_item->nbytes = cpu_to_le64(root->leafsize);
  256. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  257. btrfs_set_root_bytenr(&root_item, leaf->start);
  258. btrfs_set_root_generation(&root_item, trans->transid);
  259. btrfs_set_root_level(&root_item, 0);
  260. btrfs_set_root_refs(&root_item, 1);
  261. btrfs_set_root_used(&root_item, leaf->len);
  262. btrfs_set_root_last_snapshot(&root_item, 0);
  263. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  264. root_item.drop_level = 0;
  265. btrfs_tree_unlock(leaf);
  266. free_extent_buffer(leaf);
  267. leaf = NULL;
  268. btrfs_set_root_dirid(&root_item, new_dirid);
  269. key.objectid = objectid;
  270. key.offset = 0;
  271. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  272. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  273. &root_item);
  274. if (ret)
  275. goto fail;
  276. key.offset = (u64)-1;
  277. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  278. BUG_ON(IS_ERR(new_root));
  279. btrfs_record_root_in_trans(trans, new_root);
  280. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  281. BTRFS_I(dir)->block_group);
  282. /*
  283. * insert the directory item
  284. */
  285. ret = btrfs_set_inode_index(dir, &index);
  286. BUG_ON(ret);
  287. ret = btrfs_insert_dir_item(trans, root,
  288. name, namelen, dir->i_ino, &key,
  289. BTRFS_FT_DIR, index);
  290. if (ret)
  291. goto fail;
  292. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  293. ret = btrfs_update_inode(trans, root, dir);
  294. BUG_ON(ret);
  295. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  296. objectid, root->root_key.objectid,
  297. dir->i_ino, index, name, namelen);
  298. BUG_ON(ret);
  299. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  300. fail:
  301. dput(parent);
  302. if (async_transid) {
  303. *async_transid = trans->transid;
  304. err = btrfs_commit_transaction_async(trans, root, 1);
  305. } else {
  306. err = btrfs_commit_transaction(trans, root);
  307. }
  308. if (err && !ret)
  309. ret = err;
  310. return ret;
  311. }
  312. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  313. char *name, int namelen, u64 *async_transid,
  314. bool readonly)
  315. {
  316. struct inode *inode;
  317. struct dentry *parent;
  318. struct btrfs_pending_snapshot *pending_snapshot;
  319. struct btrfs_trans_handle *trans;
  320. int ret;
  321. if (!root->ref_cows)
  322. return -EINVAL;
  323. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  324. if (!pending_snapshot)
  325. return -ENOMEM;
  326. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  327. pending_snapshot->dentry = dentry;
  328. pending_snapshot->root = root;
  329. pending_snapshot->readonly = readonly;
  330. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  331. if (IS_ERR(trans)) {
  332. ret = PTR_ERR(trans);
  333. goto fail;
  334. }
  335. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  336. BUG_ON(ret);
  337. list_add(&pending_snapshot->list,
  338. &trans->transaction->pending_snapshots);
  339. if (async_transid) {
  340. *async_transid = trans->transid;
  341. ret = btrfs_commit_transaction_async(trans,
  342. root->fs_info->extent_root, 1);
  343. } else {
  344. ret = btrfs_commit_transaction(trans,
  345. root->fs_info->extent_root);
  346. }
  347. BUG_ON(ret);
  348. ret = pending_snapshot->error;
  349. if (ret)
  350. goto fail;
  351. btrfs_orphan_cleanup(pending_snapshot->snap);
  352. parent = dget_parent(dentry);
  353. inode = btrfs_lookup_dentry(parent->d_inode, dentry);
  354. dput(parent);
  355. if (IS_ERR(inode)) {
  356. ret = PTR_ERR(inode);
  357. goto fail;
  358. }
  359. BUG_ON(!inode);
  360. d_instantiate(dentry, inode);
  361. ret = 0;
  362. fail:
  363. kfree(pending_snapshot);
  364. return ret;
  365. }
  366. /* copy of check_sticky in fs/namei.c()
  367. * It's inline, so penalty for filesystems that don't use sticky bit is
  368. * minimal.
  369. */
  370. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  371. {
  372. uid_t fsuid = current_fsuid();
  373. if (!(dir->i_mode & S_ISVTX))
  374. return 0;
  375. if (inode->i_uid == fsuid)
  376. return 0;
  377. if (dir->i_uid == fsuid)
  378. return 0;
  379. return !capable(CAP_FOWNER);
  380. }
  381. /* copy of may_delete in fs/namei.c()
  382. * Check whether we can remove a link victim from directory dir, check
  383. * whether the type of victim is right.
  384. * 1. We can't do it if dir is read-only (done in permission())
  385. * 2. We should have write and exec permissions on dir
  386. * 3. We can't remove anything from append-only dir
  387. * 4. We can't do anything with immutable dir (done in permission())
  388. * 5. If the sticky bit on dir is set we should either
  389. * a. be owner of dir, or
  390. * b. be owner of victim, or
  391. * c. have CAP_FOWNER capability
  392. * 6. If the victim is append-only or immutable we can't do antyhing with
  393. * links pointing to it.
  394. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  395. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  396. * 9. We can't remove a root or mountpoint.
  397. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  398. * nfs_async_unlink().
  399. */
  400. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  401. {
  402. int error;
  403. if (!victim->d_inode)
  404. return -ENOENT;
  405. BUG_ON(victim->d_parent->d_inode != dir);
  406. audit_inode_child(victim, dir);
  407. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  408. if (error)
  409. return error;
  410. if (IS_APPEND(dir))
  411. return -EPERM;
  412. if (btrfs_check_sticky(dir, victim->d_inode)||
  413. IS_APPEND(victim->d_inode)||
  414. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  415. return -EPERM;
  416. if (isdir) {
  417. if (!S_ISDIR(victim->d_inode->i_mode))
  418. return -ENOTDIR;
  419. if (IS_ROOT(victim))
  420. return -EBUSY;
  421. } else if (S_ISDIR(victim->d_inode->i_mode))
  422. return -EISDIR;
  423. if (IS_DEADDIR(dir))
  424. return -ENOENT;
  425. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  426. return -EBUSY;
  427. return 0;
  428. }
  429. /* copy of may_create in fs/namei.c() */
  430. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  431. {
  432. if (child->d_inode)
  433. return -EEXIST;
  434. if (IS_DEADDIR(dir))
  435. return -ENOENT;
  436. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  437. }
  438. /*
  439. * Create a new subvolume below @parent. This is largely modeled after
  440. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  441. * inside this filesystem so it's quite a bit simpler.
  442. */
  443. static noinline int btrfs_mksubvol(struct path *parent,
  444. char *name, int namelen,
  445. struct btrfs_root *snap_src,
  446. u64 *async_transid, bool readonly)
  447. {
  448. struct inode *dir = parent->dentry->d_inode;
  449. struct dentry *dentry;
  450. int error;
  451. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  452. dentry = lookup_one_len(name, parent->dentry, namelen);
  453. error = PTR_ERR(dentry);
  454. if (IS_ERR(dentry))
  455. goto out_unlock;
  456. error = -EEXIST;
  457. if (dentry->d_inode)
  458. goto out_dput;
  459. error = mnt_want_write(parent->mnt);
  460. if (error)
  461. goto out_dput;
  462. error = btrfs_may_create(dir, dentry);
  463. if (error)
  464. goto out_drop_write;
  465. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  466. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  467. goto out_up_read;
  468. if (snap_src) {
  469. error = create_snapshot(snap_src, dentry,
  470. name, namelen, async_transid, readonly);
  471. } else {
  472. error = create_subvol(BTRFS_I(dir)->root, dentry,
  473. name, namelen, async_transid);
  474. }
  475. if (!error)
  476. fsnotify_mkdir(dir, dentry);
  477. out_up_read:
  478. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  479. out_drop_write:
  480. mnt_drop_write(parent->mnt);
  481. out_dput:
  482. dput(dentry);
  483. out_unlock:
  484. mutex_unlock(&dir->i_mutex);
  485. return error;
  486. }
  487. static int should_defrag_range(struct inode *inode, u64 start, u64 len,
  488. int thresh, u64 *last_len, u64 *skip,
  489. u64 *defrag_end)
  490. {
  491. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  492. struct extent_map *em = NULL;
  493. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  494. int ret = 1;
  495. if (thresh == 0)
  496. thresh = 256 * 1024;
  497. /*
  498. * make sure that once we start defragging and extent, we keep on
  499. * defragging it
  500. */
  501. if (start < *defrag_end)
  502. return 1;
  503. *skip = 0;
  504. /*
  505. * hopefully we have this extent in the tree already, try without
  506. * the full extent lock
  507. */
  508. read_lock(&em_tree->lock);
  509. em = lookup_extent_mapping(em_tree, start, len);
  510. read_unlock(&em_tree->lock);
  511. if (!em) {
  512. /* get the big lock and read metadata off disk */
  513. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  514. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  515. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  516. if (IS_ERR(em))
  517. return 0;
  518. }
  519. /* this will cover holes, and inline extents */
  520. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  521. ret = 0;
  522. /*
  523. * we hit a real extent, if it is big don't bother defragging it again
  524. */
  525. if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
  526. ret = 0;
  527. /*
  528. * last_len ends up being a counter of how many bytes we've defragged.
  529. * every time we choose not to defrag an extent, we reset *last_len
  530. * so that the next tiny extent will force a defrag.
  531. *
  532. * The end result of this is that tiny extents before a single big
  533. * extent will force at least part of that big extent to be defragged.
  534. */
  535. if (ret) {
  536. *last_len += len;
  537. *defrag_end = extent_map_end(em);
  538. } else {
  539. *last_len = 0;
  540. *skip = extent_map_end(em);
  541. *defrag_end = 0;
  542. }
  543. free_extent_map(em);
  544. return ret;
  545. }
  546. static int btrfs_defrag_file(struct file *file,
  547. struct btrfs_ioctl_defrag_range_args *range)
  548. {
  549. struct inode *inode = fdentry(file)->d_inode;
  550. struct btrfs_root *root = BTRFS_I(inode)->root;
  551. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  552. struct btrfs_ordered_extent *ordered;
  553. struct page *page;
  554. struct btrfs_super_block *disk_super;
  555. unsigned long last_index;
  556. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  557. unsigned long total_read = 0;
  558. u64 features;
  559. u64 page_start;
  560. u64 page_end;
  561. u64 last_len = 0;
  562. u64 skip = 0;
  563. u64 defrag_end = 0;
  564. unsigned long i;
  565. int ret;
  566. int compress_type = BTRFS_COMPRESS_ZLIB;
  567. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  568. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  569. return -EINVAL;
  570. if (range->compress_type)
  571. compress_type = range->compress_type;
  572. }
  573. if (inode->i_size == 0)
  574. return 0;
  575. if (range->start + range->len > range->start) {
  576. last_index = min_t(u64, inode->i_size - 1,
  577. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  578. } else {
  579. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  580. }
  581. i = range->start >> PAGE_CACHE_SHIFT;
  582. while (i <= last_index) {
  583. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  584. PAGE_CACHE_SIZE,
  585. range->extent_thresh,
  586. &last_len, &skip,
  587. &defrag_end)) {
  588. unsigned long next;
  589. /*
  590. * the should_defrag function tells us how much to skip
  591. * bump our counter by the suggested amount
  592. */
  593. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  594. i = max(i + 1, next);
  595. continue;
  596. }
  597. if (total_read % ra_pages == 0) {
  598. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  599. min(last_index, i + ra_pages - 1));
  600. }
  601. total_read++;
  602. mutex_lock(&inode->i_mutex);
  603. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  604. BTRFS_I(inode)->force_compress = compress_type;
  605. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  606. if (ret)
  607. goto err_unlock;
  608. again:
  609. if (inode->i_size == 0 ||
  610. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  611. ret = 0;
  612. goto err_reservations;
  613. }
  614. page = grab_cache_page(inode->i_mapping, i);
  615. if (!page) {
  616. ret = -ENOMEM;
  617. goto err_reservations;
  618. }
  619. if (!PageUptodate(page)) {
  620. btrfs_readpage(NULL, page);
  621. lock_page(page);
  622. if (!PageUptodate(page)) {
  623. unlock_page(page);
  624. page_cache_release(page);
  625. ret = -EIO;
  626. goto err_reservations;
  627. }
  628. }
  629. if (page->mapping != inode->i_mapping) {
  630. unlock_page(page);
  631. page_cache_release(page);
  632. goto again;
  633. }
  634. wait_on_page_writeback(page);
  635. if (PageDirty(page)) {
  636. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  637. goto loop_unlock;
  638. }
  639. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  640. page_end = page_start + PAGE_CACHE_SIZE - 1;
  641. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  642. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  643. if (ordered) {
  644. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  645. unlock_page(page);
  646. page_cache_release(page);
  647. btrfs_start_ordered_extent(inode, ordered, 1);
  648. btrfs_put_ordered_extent(ordered);
  649. goto again;
  650. }
  651. set_page_extent_mapped(page);
  652. /*
  653. * this makes sure page_mkwrite is called on the
  654. * page if it is dirtied again later
  655. */
  656. clear_page_dirty_for_io(page);
  657. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  658. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  659. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  660. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
  661. ClearPageChecked(page);
  662. set_page_dirty(page);
  663. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  664. loop_unlock:
  665. unlock_page(page);
  666. page_cache_release(page);
  667. mutex_unlock(&inode->i_mutex);
  668. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  669. i++;
  670. }
  671. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  672. filemap_flush(inode->i_mapping);
  673. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  674. /* the filemap_flush will queue IO into the worker threads, but
  675. * we have to make sure the IO is actually started and that
  676. * ordered extents get created before we return
  677. */
  678. atomic_inc(&root->fs_info->async_submit_draining);
  679. while (atomic_read(&root->fs_info->nr_async_submits) ||
  680. atomic_read(&root->fs_info->async_delalloc_pages)) {
  681. wait_event(root->fs_info->async_submit_wait,
  682. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  683. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  684. }
  685. atomic_dec(&root->fs_info->async_submit_draining);
  686. mutex_lock(&inode->i_mutex);
  687. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  688. mutex_unlock(&inode->i_mutex);
  689. }
  690. disk_super = &root->fs_info->super_copy;
  691. features = btrfs_super_incompat_flags(disk_super);
  692. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  693. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  694. btrfs_set_super_incompat_flags(disk_super, features);
  695. }
  696. return 0;
  697. err_reservations:
  698. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  699. err_unlock:
  700. mutex_unlock(&inode->i_mutex);
  701. return ret;
  702. }
  703. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  704. void __user *arg)
  705. {
  706. u64 new_size;
  707. u64 old_size;
  708. u64 devid = 1;
  709. struct btrfs_ioctl_vol_args *vol_args;
  710. struct btrfs_trans_handle *trans;
  711. struct btrfs_device *device = NULL;
  712. char *sizestr;
  713. char *devstr = NULL;
  714. int ret = 0;
  715. int mod = 0;
  716. if (root->fs_info->sb->s_flags & MS_RDONLY)
  717. return -EROFS;
  718. if (!capable(CAP_SYS_ADMIN))
  719. return -EPERM;
  720. vol_args = memdup_user(arg, sizeof(*vol_args));
  721. if (IS_ERR(vol_args))
  722. return PTR_ERR(vol_args);
  723. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  724. mutex_lock(&root->fs_info->volume_mutex);
  725. sizestr = vol_args->name;
  726. devstr = strchr(sizestr, ':');
  727. if (devstr) {
  728. char *end;
  729. sizestr = devstr + 1;
  730. *devstr = '\0';
  731. devstr = vol_args->name;
  732. devid = simple_strtoull(devstr, &end, 10);
  733. printk(KERN_INFO "resizing devid %llu\n",
  734. (unsigned long long)devid);
  735. }
  736. device = btrfs_find_device(root, devid, NULL, NULL);
  737. if (!device) {
  738. printk(KERN_INFO "resizer unable to find device %llu\n",
  739. (unsigned long long)devid);
  740. ret = -EINVAL;
  741. goto out_unlock;
  742. }
  743. if (!strcmp(sizestr, "max"))
  744. new_size = device->bdev->bd_inode->i_size;
  745. else {
  746. if (sizestr[0] == '-') {
  747. mod = -1;
  748. sizestr++;
  749. } else if (sizestr[0] == '+') {
  750. mod = 1;
  751. sizestr++;
  752. }
  753. new_size = memparse(sizestr, NULL);
  754. if (new_size == 0) {
  755. ret = -EINVAL;
  756. goto out_unlock;
  757. }
  758. }
  759. old_size = device->total_bytes;
  760. if (mod < 0) {
  761. if (new_size > old_size) {
  762. ret = -EINVAL;
  763. goto out_unlock;
  764. }
  765. new_size = old_size - new_size;
  766. } else if (mod > 0) {
  767. new_size = old_size + new_size;
  768. }
  769. if (new_size < 256 * 1024 * 1024) {
  770. ret = -EINVAL;
  771. goto out_unlock;
  772. }
  773. if (new_size > device->bdev->bd_inode->i_size) {
  774. ret = -EFBIG;
  775. goto out_unlock;
  776. }
  777. do_div(new_size, root->sectorsize);
  778. new_size *= root->sectorsize;
  779. printk(KERN_INFO "new size for %s is %llu\n",
  780. device->name, (unsigned long long)new_size);
  781. if (new_size > old_size) {
  782. trans = btrfs_start_transaction(root, 0);
  783. if (IS_ERR(trans)) {
  784. ret = PTR_ERR(trans);
  785. goto out_unlock;
  786. }
  787. ret = btrfs_grow_device(trans, device, new_size);
  788. btrfs_commit_transaction(trans, root);
  789. } else {
  790. ret = btrfs_shrink_device(device, new_size);
  791. }
  792. out_unlock:
  793. mutex_unlock(&root->fs_info->volume_mutex);
  794. kfree(vol_args);
  795. return ret;
  796. }
  797. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  798. char *name,
  799. unsigned long fd,
  800. int subvol,
  801. u64 *transid,
  802. bool readonly)
  803. {
  804. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  805. struct file *src_file;
  806. int namelen;
  807. int ret = 0;
  808. if (root->fs_info->sb->s_flags & MS_RDONLY)
  809. return -EROFS;
  810. namelen = strlen(name);
  811. if (strchr(name, '/')) {
  812. ret = -EINVAL;
  813. goto out;
  814. }
  815. if (subvol) {
  816. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  817. NULL, transid, readonly);
  818. } else {
  819. struct inode *src_inode;
  820. src_file = fget(fd);
  821. if (!src_file) {
  822. ret = -EINVAL;
  823. goto out;
  824. }
  825. src_inode = src_file->f_path.dentry->d_inode;
  826. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  827. printk(KERN_INFO "btrfs: Snapshot src from "
  828. "another FS\n");
  829. ret = -EINVAL;
  830. fput(src_file);
  831. goto out;
  832. }
  833. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  834. BTRFS_I(src_inode)->root,
  835. transid, readonly);
  836. fput(src_file);
  837. }
  838. out:
  839. return ret;
  840. }
  841. static noinline int btrfs_ioctl_snap_create(struct file *file,
  842. void __user *arg, int subvol)
  843. {
  844. struct btrfs_ioctl_vol_args *vol_args;
  845. int ret;
  846. vol_args = memdup_user(arg, sizeof(*vol_args));
  847. if (IS_ERR(vol_args))
  848. return PTR_ERR(vol_args);
  849. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  850. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  851. vol_args->fd, subvol,
  852. NULL, false);
  853. kfree(vol_args);
  854. return ret;
  855. }
  856. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  857. void __user *arg, int subvol)
  858. {
  859. struct btrfs_ioctl_vol_args_v2 *vol_args;
  860. int ret;
  861. u64 transid = 0;
  862. u64 *ptr = NULL;
  863. bool readonly = false;
  864. vol_args = memdup_user(arg, sizeof(*vol_args));
  865. if (IS_ERR(vol_args))
  866. return PTR_ERR(vol_args);
  867. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  868. if (vol_args->flags &
  869. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  870. ret = -EOPNOTSUPP;
  871. goto out;
  872. }
  873. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  874. ptr = &transid;
  875. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  876. readonly = true;
  877. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  878. vol_args->fd, subvol,
  879. ptr, readonly);
  880. if (ret == 0 && ptr &&
  881. copy_to_user(arg +
  882. offsetof(struct btrfs_ioctl_vol_args_v2,
  883. transid), ptr, sizeof(*ptr)))
  884. ret = -EFAULT;
  885. out:
  886. kfree(vol_args);
  887. return ret;
  888. }
  889. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  890. void __user *arg)
  891. {
  892. struct inode *inode = fdentry(file)->d_inode;
  893. struct btrfs_root *root = BTRFS_I(inode)->root;
  894. int ret = 0;
  895. u64 flags = 0;
  896. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  897. return -EINVAL;
  898. down_read(&root->fs_info->subvol_sem);
  899. if (btrfs_root_readonly(root))
  900. flags |= BTRFS_SUBVOL_RDONLY;
  901. up_read(&root->fs_info->subvol_sem);
  902. if (copy_to_user(arg, &flags, sizeof(flags)))
  903. ret = -EFAULT;
  904. return ret;
  905. }
  906. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  907. void __user *arg)
  908. {
  909. struct inode *inode = fdentry(file)->d_inode;
  910. struct btrfs_root *root = BTRFS_I(inode)->root;
  911. struct btrfs_trans_handle *trans;
  912. u64 root_flags;
  913. u64 flags;
  914. int ret = 0;
  915. if (root->fs_info->sb->s_flags & MS_RDONLY)
  916. return -EROFS;
  917. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  918. return -EINVAL;
  919. if (copy_from_user(&flags, arg, sizeof(flags)))
  920. return -EFAULT;
  921. if (flags & ~BTRFS_SUBVOL_CREATE_ASYNC)
  922. return -EINVAL;
  923. if (flags & ~BTRFS_SUBVOL_RDONLY)
  924. return -EOPNOTSUPP;
  925. down_write(&root->fs_info->subvol_sem);
  926. /* nothing to do */
  927. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  928. goto out;
  929. root_flags = btrfs_root_flags(&root->root_item);
  930. if (flags & BTRFS_SUBVOL_RDONLY)
  931. btrfs_set_root_flags(&root->root_item,
  932. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  933. else
  934. btrfs_set_root_flags(&root->root_item,
  935. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  936. trans = btrfs_start_transaction(root, 1);
  937. if (IS_ERR(trans)) {
  938. ret = PTR_ERR(trans);
  939. goto out_reset;
  940. }
  941. ret = btrfs_update_root(trans, root,
  942. &root->root_key, &root->root_item);
  943. btrfs_commit_transaction(trans, root);
  944. out_reset:
  945. if (ret)
  946. btrfs_set_root_flags(&root->root_item, root_flags);
  947. out:
  948. up_write(&root->fs_info->subvol_sem);
  949. return ret;
  950. }
  951. /*
  952. * helper to check if the subvolume references other subvolumes
  953. */
  954. static noinline int may_destroy_subvol(struct btrfs_root *root)
  955. {
  956. struct btrfs_path *path;
  957. struct btrfs_key key;
  958. int ret;
  959. path = btrfs_alloc_path();
  960. if (!path)
  961. return -ENOMEM;
  962. key.objectid = root->root_key.objectid;
  963. key.type = BTRFS_ROOT_REF_KEY;
  964. key.offset = (u64)-1;
  965. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  966. &key, path, 0, 0);
  967. if (ret < 0)
  968. goto out;
  969. BUG_ON(ret == 0);
  970. ret = 0;
  971. if (path->slots[0] > 0) {
  972. path->slots[0]--;
  973. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  974. if (key.objectid == root->root_key.objectid &&
  975. key.type == BTRFS_ROOT_REF_KEY)
  976. ret = -ENOTEMPTY;
  977. }
  978. out:
  979. btrfs_free_path(path);
  980. return ret;
  981. }
  982. static noinline int key_in_sk(struct btrfs_key *key,
  983. struct btrfs_ioctl_search_key *sk)
  984. {
  985. struct btrfs_key test;
  986. int ret;
  987. test.objectid = sk->min_objectid;
  988. test.type = sk->min_type;
  989. test.offset = sk->min_offset;
  990. ret = btrfs_comp_cpu_keys(key, &test);
  991. if (ret < 0)
  992. return 0;
  993. test.objectid = sk->max_objectid;
  994. test.type = sk->max_type;
  995. test.offset = sk->max_offset;
  996. ret = btrfs_comp_cpu_keys(key, &test);
  997. if (ret > 0)
  998. return 0;
  999. return 1;
  1000. }
  1001. static noinline int copy_to_sk(struct btrfs_root *root,
  1002. struct btrfs_path *path,
  1003. struct btrfs_key *key,
  1004. struct btrfs_ioctl_search_key *sk,
  1005. char *buf,
  1006. unsigned long *sk_offset,
  1007. int *num_found)
  1008. {
  1009. u64 found_transid;
  1010. struct extent_buffer *leaf;
  1011. struct btrfs_ioctl_search_header sh;
  1012. unsigned long item_off;
  1013. unsigned long item_len;
  1014. int nritems;
  1015. int i;
  1016. int slot;
  1017. int found = 0;
  1018. int ret = 0;
  1019. leaf = path->nodes[0];
  1020. slot = path->slots[0];
  1021. nritems = btrfs_header_nritems(leaf);
  1022. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1023. i = nritems;
  1024. goto advance_key;
  1025. }
  1026. found_transid = btrfs_header_generation(leaf);
  1027. for (i = slot; i < nritems; i++) {
  1028. item_off = btrfs_item_ptr_offset(leaf, i);
  1029. item_len = btrfs_item_size_nr(leaf, i);
  1030. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1031. item_len = 0;
  1032. if (sizeof(sh) + item_len + *sk_offset >
  1033. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1034. ret = 1;
  1035. goto overflow;
  1036. }
  1037. btrfs_item_key_to_cpu(leaf, key, i);
  1038. if (!key_in_sk(key, sk))
  1039. continue;
  1040. sh.objectid = key->objectid;
  1041. sh.offset = key->offset;
  1042. sh.type = key->type;
  1043. sh.len = item_len;
  1044. sh.transid = found_transid;
  1045. /* copy search result header */
  1046. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1047. *sk_offset += sizeof(sh);
  1048. if (item_len) {
  1049. char *p = buf + *sk_offset;
  1050. /* copy the item */
  1051. read_extent_buffer(leaf, p,
  1052. item_off, item_len);
  1053. *sk_offset += item_len;
  1054. }
  1055. found++;
  1056. if (*num_found >= sk->nr_items)
  1057. break;
  1058. }
  1059. advance_key:
  1060. ret = 0;
  1061. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1062. key->offset++;
  1063. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1064. key->offset = 0;
  1065. key->type++;
  1066. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1067. key->offset = 0;
  1068. key->type = 0;
  1069. key->objectid++;
  1070. } else
  1071. ret = 1;
  1072. overflow:
  1073. *num_found += found;
  1074. return ret;
  1075. }
  1076. static noinline int search_ioctl(struct inode *inode,
  1077. struct btrfs_ioctl_search_args *args)
  1078. {
  1079. struct btrfs_root *root;
  1080. struct btrfs_key key;
  1081. struct btrfs_key max_key;
  1082. struct btrfs_path *path;
  1083. struct btrfs_ioctl_search_key *sk = &args->key;
  1084. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1085. int ret;
  1086. int num_found = 0;
  1087. unsigned long sk_offset = 0;
  1088. path = btrfs_alloc_path();
  1089. if (!path)
  1090. return -ENOMEM;
  1091. if (sk->tree_id == 0) {
  1092. /* search the root of the inode that was passed */
  1093. root = BTRFS_I(inode)->root;
  1094. } else {
  1095. key.objectid = sk->tree_id;
  1096. key.type = BTRFS_ROOT_ITEM_KEY;
  1097. key.offset = (u64)-1;
  1098. root = btrfs_read_fs_root_no_name(info, &key);
  1099. if (IS_ERR(root)) {
  1100. printk(KERN_ERR "could not find root %llu\n",
  1101. sk->tree_id);
  1102. btrfs_free_path(path);
  1103. return -ENOENT;
  1104. }
  1105. }
  1106. key.objectid = sk->min_objectid;
  1107. key.type = sk->min_type;
  1108. key.offset = sk->min_offset;
  1109. max_key.objectid = sk->max_objectid;
  1110. max_key.type = sk->max_type;
  1111. max_key.offset = sk->max_offset;
  1112. path->keep_locks = 1;
  1113. while(1) {
  1114. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1115. sk->min_transid);
  1116. if (ret != 0) {
  1117. if (ret > 0)
  1118. ret = 0;
  1119. goto err;
  1120. }
  1121. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1122. &sk_offset, &num_found);
  1123. btrfs_release_path(root, path);
  1124. if (ret || num_found >= sk->nr_items)
  1125. break;
  1126. }
  1127. ret = 0;
  1128. err:
  1129. sk->nr_items = num_found;
  1130. btrfs_free_path(path);
  1131. return ret;
  1132. }
  1133. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1134. void __user *argp)
  1135. {
  1136. struct btrfs_ioctl_search_args *args;
  1137. struct inode *inode;
  1138. int ret;
  1139. if (!capable(CAP_SYS_ADMIN))
  1140. return -EPERM;
  1141. args = memdup_user(argp, sizeof(*args));
  1142. if (IS_ERR(args))
  1143. return PTR_ERR(args);
  1144. inode = fdentry(file)->d_inode;
  1145. ret = search_ioctl(inode, args);
  1146. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1147. ret = -EFAULT;
  1148. kfree(args);
  1149. return ret;
  1150. }
  1151. /*
  1152. * Search INODE_REFs to identify path name of 'dirid' directory
  1153. * in a 'tree_id' tree. and sets path name to 'name'.
  1154. */
  1155. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1156. u64 tree_id, u64 dirid, char *name)
  1157. {
  1158. struct btrfs_root *root;
  1159. struct btrfs_key key;
  1160. char *ptr;
  1161. int ret = -1;
  1162. int slot;
  1163. int len;
  1164. int total_len = 0;
  1165. struct btrfs_inode_ref *iref;
  1166. struct extent_buffer *l;
  1167. struct btrfs_path *path;
  1168. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1169. name[0]='\0';
  1170. return 0;
  1171. }
  1172. path = btrfs_alloc_path();
  1173. if (!path)
  1174. return -ENOMEM;
  1175. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1176. key.objectid = tree_id;
  1177. key.type = BTRFS_ROOT_ITEM_KEY;
  1178. key.offset = (u64)-1;
  1179. root = btrfs_read_fs_root_no_name(info, &key);
  1180. if (IS_ERR(root)) {
  1181. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1182. ret = -ENOENT;
  1183. goto out;
  1184. }
  1185. key.objectid = dirid;
  1186. key.type = BTRFS_INODE_REF_KEY;
  1187. key.offset = (u64)-1;
  1188. while(1) {
  1189. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1190. if (ret < 0)
  1191. goto out;
  1192. l = path->nodes[0];
  1193. slot = path->slots[0];
  1194. if (ret > 0 && slot > 0)
  1195. slot--;
  1196. btrfs_item_key_to_cpu(l, &key, slot);
  1197. if (ret > 0 && (key.objectid != dirid ||
  1198. key.type != BTRFS_INODE_REF_KEY)) {
  1199. ret = -ENOENT;
  1200. goto out;
  1201. }
  1202. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1203. len = btrfs_inode_ref_name_len(l, iref);
  1204. ptr -= len + 1;
  1205. total_len += len + 1;
  1206. if (ptr < name)
  1207. goto out;
  1208. *(ptr + len) = '/';
  1209. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1210. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1211. break;
  1212. btrfs_release_path(root, path);
  1213. key.objectid = key.offset;
  1214. key.offset = (u64)-1;
  1215. dirid = key.objectid;
  1216. }
  1217. if (ptr < name)
  1218. goto out;
  1219. memcpy(name, ptr, total_len);
  1220. name[total_len]='\0';
  1221. ret = 0;
  1222. out:
  1223. btrfs_free_path(path);
  1224. return ret;
  1225. }
  1226. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1227. void __user *argp)
  1228. {
  1229. struct btrfs_ioctl_ino_lookup_args *args;
  1230. struct inode *inode;
  1231. int ret;
  1232. if (!capable(CAP_SYS_ADMIN))
  1233. return -EPERM;
  1234. args = memdup_user(argp, sizeof(*args));
  1235. if (IS_ERR(args))
  1236. return PTR_ERR(args);
  1237. inode = fdentry(file)->d_inode;
  1238. if (args->treeid == 0)
  1239. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1240. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1241. args->treeid, args->objectid,
  1242. args->name);
  1243. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1244. ret = -EFAULT;
  1245. kfree(args);
  1246. return ret;
  1247. }
  1248. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1249. void __user *arg)
  1250. {
  1251. struct dentry *parent = fdentry(file);
  1252. struct dentry *dentry;
  1253. struct inode *dir = parent->d_inode;
  1254. struct inode *inode;
  1255. struct btrfs_root *root = BTRFS_I(dir)->root;
  1256. struct btrfs_root *dest = NULL;
  1257. struct btrfs_ioctl_vol_args *vol_args;
  1258. struct btrfs_trans_handle *trans;
  1259. int namelen;
  1260. int ret;
  1261. int err = 0;
  1262. vol_args = memdup_user(arg, sizeof(*vol_args));
  1263. if (IS_ERR(vol_args))
  1264. return PTR_ERR(vol_args);
  1265. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1266. namelen = strlen(vol_args->name);
  1267. if (strchr(vol_args->name, '/') ||
  1268. strncmp(vol_args->name, "..", namelen) == 0) {
  1269. err = -EINVAL;
  1270. goto out;
  1271. }
  1272. err = mnt_want_write(file->f_path.mnt);
  1273. if (err)
  1274. goto out;
  1275. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1276. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1277. if (IS_ERR(dentry)) {
  1278. err = PTR_ERR(dentry);
  1279. goto out_unlock_dir;
  1280. }
  1281. if (!dentry->d_inode) {
  1282. err = -ENOENT;
  1283. goto out_dput;
  1284. }
  1285. inode = dentry->d_inode;
  1286. dest = BTRFS_I(inode)->root;
  1287. if (!capable(CAP_SYS_ADMIN)){
  1288. /*
  1289. * Regular user. Only allow this with a special mount
  1290. * option, when the user has write+exec access to the
  1291. * subvol root, and when rmdir(2) would have been
  1292. * allowed.
  1293. *
  1294. * Note that this is _not_ check that the subvol is
  1295. * empty or doesn't contain data that we wouldn't
  1296. * otherwise be able to delete.
  1297. *
  1298. * Users who want to delete empty subvols should try
  1299. * rmdir(2).
  1300. */
  1301. err = -EPERM;
  1302. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1303. goto out_dput;
  1304. /*
  1305. * Do not allow deletion if the parent dir is the same
  1306. * as the dir to be deleted. That means the ioctl
  1307. * must be called on the dentry referencing the root
  1308. * of the subvol, not a random directory contained
  1309. * within it.
  1310. */
  1311. err = -EINVAL;
  1312. if (root == dest)
  1313. goto out_dput;
  1314. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1315. if (err)
  1316. goto out_dput;
  1317. /* check if subvolume may be deleted by a non-root user */
  1318. err = btrfs_may_delete(dir, dentry, 1);
  1319. if (err)
  1320. goto out_dput;
  1321. }
  1322. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1323. err = -EINVAL;
  1324. goto out_dput;
  1325. }
  1326. mutex_lock(&inode->i_mutex);
  1327. err = d_invalidate(dentry);
  1328. if (err)
  1329. goto out_unlock;
  1330. down_write(&root->fs_info->subvol_sem);
  1331. err = may_destroy_subvol(dest);
  1332. if (err)
  1333. goto out_up_write;
  1334. trans = btrfs_start_transaction(root, 0);
  1335. if (IS_ERR(trans)) {
  1336. err = PTR_ERR(trans);
  1337. goto out_up_write;
  1338. }
  1339. trans->block_rsv = &root->fs_info->global_block_rsv;
  1340. ret = btrfs_unlink_subvol(trans, root, dir,
  1341. dest->root_key.objectid,
  1342. dentry->d_name.name,
  1343. dentry->d_name.len);
  1344. BUG_ON(ret);
  1345. btrfs_record_root_in_trans(trans, dest);
  1346. memset(&dest->root_item.drop_progress, 0,
  1347. sizeof(dest->root_item.drop_progress));
  1348. dest->root_item.drop_level = 0;
  1349. btrfs_set_root_refs(&dest->root_item, 0);
  1350. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1351. ret = btrfs_insert_orphan_item(trans,
  1352. root->fs_info->tree_root,
  1353. dest->root_key.objectid);
  1354. BUG_ON(ret);
  1355. }
  1356. ret = btrfs_end_transaction(trans, root);
  1357. BUG_ON(ret);
  1358. inode->i_flags |= S_DEAD;
  1359. out_up_write:
  1360. up_write(&root->fs_info->subvol_sem);
  1361. out_unlock:
  1362. mutex_unlock(&inode->i_mutex);
  1363. if (!err) {
  1364. shrink_dcache_sb(root->fs_info->sb);
  1365. btrfs_invalidate_inodes(dest);
  1366. d_delete(dentry);
  1367. }
  1368. out_dput:
  1369. dput(dentry);
  1370. out_unlock_dir:
  1371. mutex_unlock(&dir->i_mutex);
  1372. mnt_drop_write(file->f_path.mnt);
  1373. out:
  1374. kfree(vol_args);
  1375. return err;
  1376. }
  1377. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1378. {
  1379. struct inode *inode = fdentry(file)->d_inode;
  1380. struct btrfs_root *root = BTRFS_I(inode)->root;
  1381. struct btrfs_ioctl_defrag_range_args *range;
  1382. int ret;
  1383. if (btrfs_root_readonly(root))
  1384. return -EROFS;
  1385. ret = mnt_want_write(file->f_path.mnt);
  1386. if (ret)
  1387. return ret;
  1388. switch (inode->i_mode & S_IFMT) {
  1389. case S_IFDIR:
  1390. if (!capable(CAP_SYS_ADMIN)) {
  1391. ret = -EPERM;
  1392. goto out;
  1393. }
  1394. ret = btrfs_defrag_root(root, 0);
  1395. if (ret)
  1396. goto out;
  1397. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1398. break;
  1399. case S_IFREG:
  1400. if (!(file->f_mode & FMODE_WRITE)) {
  1401. ret = -EINVAL;
  1402. goto out;
  1403. }
  1404. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1405. if (!range) {
  1406. ret = -ENOMEM;
  1407. goto out;
  1408. }
  1409. if (argp) {
  1410. if (copy_from_user(range, argp,
  1411. sizeof(*range))) {
  1412. ret = -EFAULT;
  1413. kfree(range);
  1414. goto out;
  1415. }
  1416. /* compression requires us to start the IO */
  1417. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1418. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1419. range->extent_thresh = (u32)-1;
  1420. }
  1421. } else {
  1422. /* the rest are all set to zero by kzalloc */
  1423. range->len = (u64)-1;
  1424. }
  1425. ret = btrfs_defrag_file(file, range);
  1426. kfree(range);
  1427. break;
  1428. default:
  1429. ret = -EINVAL;
  1430. }
  1431. out:
  1432. mnt_drop_write(file->f_path.mnt);
  1433. return ret;
  1434. }
  1435. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1436. {
  1437. struct btrfs_ioctl_vol_args *vol_args;
  1438. int ret;
  1439. if (!capable(CAP_SYS_ADMIN))
  1440. return -EPERM;
  1441. vol_args = memdup_user(arg, sizeof(*vol_args));
  1442. if (IS_ERR(vol_args))
  1443. return PTR_ERR(vol_args);
  1444. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1445. ret = btrfs_init_new_device(root, vol_args->name);
  1446. kfree(vol_args);
  1447. return ret;
  1448. }
  1449. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1450. {
  1451. struct btrfs_ioctl_vol_args *vol_args;
  1452. int ret;
  1453. if (!capable(CAP_SYS_ADMIN))
  1454. return -EPERM;
  1455. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1456. return -EROFS;
  1457. vol_args = memdup_user(arg, sizeof(*vol_args));
  1458. if (IS_ERR(vol_args))
  1459. return PTR_ERR(vol_args);
  1460. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1461. ret = btrfs_rm_device(root, vol_args->name);
  1462. kfree(vol_args);
  1463. return ret;
  1464. }
  1465. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1466. u64 off, u64 olen, u64 destoff)
  1467. {
  1468. struct inode *inode = fdentry(file)->d_inode;
  1469. struct btrfs_root *root = BTRFS_I(inode)->root;
  1470. struct file *src_file;
  1471. struct inode *src;
  1472. struct btrfs_trans_handle *trans;
  1473. struct btrfs_path *path;
  1474. struct extent_buffer *leaf;
  1475. char *buf;
  1476. struct btrfs_key key;
  1477. u32 nritems;
  1478. int slot;
  1479. int ret;
  1480. u64 len = olen;
  1481. u64 bs = root->fs_info->sb->s_blocksize;
  1482. u64 hint_byte;
  1483. /*
  1484. * TODO:
  1485. * - split compressed inline extents. annoying: we need to
  1486. * decompress into destination's address_space (the file offset
  1487. * may change, so source mapping won't do), then recompress (or
  1488. * otherwise reinsert) a subrange.
  1489. * - allow ranges within the same file to be cloned (provided
  1490. * they don't overlap)?
  1491. */
  1492. /* the destination must be opened for writing */
  1493. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1494. return -EINVAL;
  1495. if (btrfs_root_readonly(root))
  1496. return -EROFS;
  1497. ret = mnt_want_write(file->f_path.mnt);
  1498. if (ret)
  1499. return ret;
  1500. src_file = fget(srcfd);
  1501. if (!src_file) {
  1502. ret = -EBADF;
  1503. goto out_drop_write;
  1504. }
  1505. src = src_file->f_dentry->d_inode;
  1506. ret = -EINVAL;
  1507. if (src == inode)
  1508. goto out_fput;
  1509. /* the src must be open for reading */
  1510. if (!(src_file->f_mode & FMODE_READ))
  1511. goto out_fput;
  1512. ret = -EISDIR;
  1513. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1514. goto out_fput;
  1515. ret = -EXDEV;
  1516. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1517. goto out_fput;
  1518. ret = -ENOMEM;
  1519. buf = vmalloc(btrfs_level_size(root, 0));
  1520. if (!buf)
  1521. goto out_fput;
  1522. path = btrfs_alloc_path();
  1523. if (!path) {
  1524. vfree(buf);
  1525. goto out_fput;
  1526. }
  1527. path->reada = 2;
  1528. if (inode < src) {
  1529. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1530. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1531. } else {
  1532. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1533. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1534. }
  1535. /* determine range to clone */
  1536. ret = -EINVAL;
  1537. if (off + len > src->i_size || off + len < off)
  1538. goto out_unlock;
  1539. if (len == 0)
  1540. olen = len = src->i_size - off;
  1541. /* if we extend to eof, continue to block boundary */
  1542. if (off + len == src->i_size)
  1543. len = ALIGN(src->i_size, bs) - off;
  1544. /* verify the end result is block aligned */
  1545. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  1546. !IS_ALIGNED(destoff, bs))
  1547. goto out_unlock;
  1548. /* do any pending delalloc/csum calc on src, one way or
  1549. another, and lock file content */
  1550. while (1) {
  1551. struct btrfs_ordered_extent *ordered;
  1552. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1553. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1554. if (!ordered &&
  1555. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1556. EXTENT_DELALLOC, 0, NULL))
  1557. break;
  1558. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1559. if (ordered)
  1560. btrfs_put_ordered_extent(ordered);
  1561. btrfs_wait_ordered_range(src, off, len);
  1562. }
  1563. /* clone data */
  1564. key.objectid = src->i_ino;
  1565. key.type = BTRFS_EXTENT_DATA_KEY;
  1566. key.offset = 0;
  1567. while (1) {
  1568. /*
  1569. * note the key will change type as we walk through the
  1570. * tree.
  1571. */
  1572. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1573. if (ret < 0)
  1574. goto out;
  1575. nritems = btrfs_header_nritems(path->nodes[0]);
  1576. if (path->slots[0] >= nritems) {
  1577. ret = btrfs_next_leaf(root, path);
  1578. if (ret < 0)
  1579. goto out;
  1580. if (ret > 0)
  1581. break;
  1582. nritems = btrfs_header_nritems(path->nodes[0]);
  1583. }
  1584. leaf = path->nodes[0];
  1585. slot = path->slots[0];
  1586. btrfs_item_key_to_cpu(leaf, &key, slot);
  1587. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1588. key.objectid != src->i_ino)
  1589. break;
  1590. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1591. struct btrfs_file_extent_item *extent;
  1592. int type;
  1593. u32 size;
  1594. struct btrfs_key new_key;
  1595. u64 disko = 0, diskl = 0;
  1596. u64 datao = 0, datal = 0;
  1597. u8 comp;
  1598. u64 endoff;
  1599. size = btrfs_item_size_nr(leaf, slot);
  1600. read_extent_buffer(leaf, buf,
  1601. btrfs_item_ptr_offset(leaf, slot),
  1602. size);
  1603. extent = btrfs_item_ptr(leaf, slot,
  1604. struct btrfs_file_extent_item);
  1605. comp = btrfs_file_extent_compression(leaf, extent);
  1606. type = btrfs_file_extent_type(leaf, extent);
  1607. if (type == BTRFS_FILE_EXTENT_REG ||
  1608. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1609. disko = btrfs_file_extent_disk_bytenr(leaf,
  1610. extent);
  1611. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1612. extent);
  1613. datao = btrfs_file_extent_offset(leaf, extent);
  1614. datal = btrfs_file_extent_num_bytes(leaf,
  1615. extent);
  1616. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1617. /* take upper bound, may be compressed */
  1618. datal = btrfs_file_extent_ram_bytes(leaf,
  1619. extent);
  1620. }
  1621. btrfs_release_path(root, path);
  1622. if (key.offset + datal <= off ||
  1623. key.offset >= off+len)
  1624. goto next;
  1625. memcpy(&new_key, &key, sizeof(new_key));
  1626. new_key.objectid = inode->i_ino;
  1627. if (off <= key.offset)
  1628. new_key.offset = key.offset + destoff - off;
  1629. else
  1630. new_key.offset = destoff;
  1631. trans = btrfs_start_transaction(root, 1);
  1632. if (IS_ERR(trans)) {
  1633. ret = PTR_ERR(trans);
  1634. goto out;
  1635. }
  1636. if (type == BTRFS_FILE_EXTENT_REG ||
  1637. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1638. if (off > key.offset) {
  1639. datao += off - key.offset;
  1640. datal -= off - key.offset;
  1641. }
  1642. if (key.offset + datal > off + len)
  1643. datal = off + len - key.offset;
  1644. ret = btrfs_drop_extents(trans, inode,
  1645. new_key.offset,
  1646. new_key.offset + datal,
  1647. &hint_byte, 1);
  1648. BUG_ON(ret);
  1649. ret = btrfs_insert_empty_item(trans, root, path,
  1650. &new_key, size);
  1651. BUG_ON(ret);
  1652. leaf = path->nodes[0];
  1653. slot = path->slots[0];
  1654. write_extent_buffer(leaf, buf,
  1655. btrfs_item_ptr_offset(leaf, slot),
  1656. size);
  1657. extent = btrfs_item_ptr(leaf, slot,
  1658. struct btrfs_file_extent_item);
  1659. /* disko == 0 means it's a hole */
  1660. if (!disko)
  1661. datao = 0;
  1662. btrfs_set_file_extent_offset(leaf, extent,
  1663. datao);
  1664. btrfs_set_file_extent_num_bytes(leaf, extent,
  1665. datal);
  1666. if (disko) {
  1667. inode_add_bytes(inode, datal);
  1668. ret = btrfs_inc_extent_ref(trans, root,
  1669. disko, diskl, 0,
  1670. root->root_key.objectid,
  1671. inode->i_ino,
  1672. new_key.offset - datao);
  1673. BUG_ON(ret);
  1674. }
  1675. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1676. u64 skip = 0;
  1677. u64 trim = 0;
  1678. if (off > key.offset) {
  1679. skip = off - key.offset;
  1680. new_key.offset += skip;
  1681. }
  1682. if (key.offset + datal > off+len)
  1683. trim = key.offset + datal - (off+len);
  1684. if (comp && (skip || trim)) {
  1685. ret = -EINVAL;
  1686. btrfs_end_transaction(trans, root);
  1687. goto out;
  1688. }
  1689. size -= skip + trim;
  1690. datal -= skip + trim;
  1691. ret = btrfs_drop_extents(trans, inode,
  1692. new_key.offset,
  1693. new_key.offset + datal,
  1694. &hint_byte, 1);
  1695. BUG_ON(ret);
  1696. ret = btrfs_insert_empty_item(trans, root, path,
  1697. &new_key, size);
  1698. BUG_ON(ret);
  1699. if (skip) {
  1700. u32 start =
  1701. btrfs_file_extent_calc_inline_size(0);
  1702. memmove(buf+start, buf+start+skip,
  1703. datal);
  1704. }
  1705. leaf = path->nodes[0];
  1706. slot = path->slots[0];
  1707. write_extent_buffer(leaf, buf,
  1708. btrfs_item_ptr_offset(leaf, slot),
  1709. size);
  1710. inode_add_bytes(inode, datal);
  1711. }
  1712. btrfs_mark_buffer_dirty(leaf);
  1713. btrfs_release_path(root, path);
  1714. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1715. /*
  1716. * we round up to the block size at eof when
  1717. * determining which extents to clone above,
  1718. * but shouldn't round up the file size
  1719. */
  1720. endoff = new_key.offset + datal;
  1721. if (endoff > destoff+olen)
  1722. endoff = destoff+olen;
  1723. if (endoff > inode->i_size)
  1724. btrfs_i_size_write(inode, endoff);
  1725. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1726. ret = btrfs_update_inode(trans, root, inode);
  1727. BUG_ON(ret);
  1728. btrfs_end_transaction(trans, root);
  1729. }
  1730. next:
  1731. btrfs_release_path(root, path);
  1732. key.offset++;
  1733. }
  1734. ret = 0;
  1735. out:
  1736. btrfs_release_path(root, path);
  1737. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1738. out_unlock:
  1739. mutex_unlock(&src->i_mutex);
  1740. mutex_unlock(&inode->i_mutex);
  1741. vfree(buf);
  1742. btrfs_free_path(path);
  1743. out_fput:
  1744. fput(src_file);
  1745. out_drop_write:
  1746. mnt_drop_write(file->f_path.mnt);
  1747. return ret;
  1748. }
  1749. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1750. {
  1751. struct btrfs_ioctl_clone_range_args args;
  1752. if (copy_from_user(&args, argp, sizeof(args)))
  1753. return -EFAULT;
  1754. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1755. args.src_length, args.dest_offset);
  1756. }
  1757. /*
  1758. * there are many ways the trans_start and trans_end ioctls can lead
  1759. * to deadlocks. They should only be used by applications that
  1760. * basically own the machine, and have a very in depth understanding
  1761. * of all the possible deadlocks and enospc problems.
  1762. */
  1763. static long btrfs_ioctl_trans_start(struct file *file)
  1764. {
  1765. struct inode *inode = fdentry(file)->d_inode;
  1766. struct btrfs_root *root = BTRFS_I(inode)->root;
  1767. struct btrfs_trans_handle *trans;
  1768. int ret;
  1769. ret = -EPERM;
  1770. if (!capable(CAP_SYS_ADMIN))
  1771. goto out;
  1772. ret = -EINPROGRESS;
  1773. if (file->private_data)
  1774. goto out;
  1775. ret = -EROFS;
  1776. if (btrfs_root_readonly(root))
  1777. goto out;
  1778. ret = mnt_want_write(file->f_path.mnt);
  1779. if (ret)
  1780. goto out;
  1781. mutex_lock(&root->fs_info->trans_mutex);
  1782. root->fs_info->open_ioctl_trans++;
  1783. mutex_unlock(&root->fs_info->trans_mutex);
  1784. ret = -ENOMEM;
  1785. trans = btrfs_start_ioctl_transaction(root, 0);
  1786. if (IS_ERR(trans))
  1787. goto out_drop;
  1788. file->private_data = trans;
  1789. return 0;
  1790. out_drop:
  1791. mutex_lock(&root->fs_info->trans_mutex);
  1792. root->fs_info->open_ioctl_trans--;
  1793. mutex_unlock(&root->fs_info->trans_mutex);
  1794. mnt_drop_write(file->f_path.mnt);
  1795. out:
  1796. return ret;
  1797. }
  1798. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1799. {
  1800. struct inode *inode = fdentry(file)->d_inode;
  1801. struct btrfs_root *root = BTRFS_I(inode)->root;
  1802. struct btrfs_root *new_root;
  1803. struct btrfs_dir_item *di;
  1804. struct btrfs_trans_handle *trans;
  1805. struct btrfs_path *path;
  1806. struct btrfs_key location;
  1807. struct btrfs_disk_key disk_key;
  1808. struct btrfs_super_block *disk_super;
  1809. u64 features;
  1810. u64 objectid = 0;
  1811. u64 dir_id;
  1812. if (!capable(CAP_SYS_ADMIN))
  1813. return -EPERM;
  1814. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1815. return -EFAULT;
  1816. if (!objectid)
  1817. objectid = root->root_key.objectid;
  1818. location.objectid = objectid;
  1819. location.type = BTRFS_ROOT_ITEM_KEY;
  1820. location.offset = (u64)-1;
  1821. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1822. if (IS_ERR(new_root))
  1823. return PTR_ERR(new_root);
  1824. if (btrfs_root_refs(&new_root->root_item) == 0)
  1825. return -ENOENT;
  1826. path = btrfs_alloc_path();
  1827. if (!path)
  1828. return -ENOMEM;
  1829. path->leave_spinning = 1;
  1830. trans = btrfs_start_transaction(root, 1);
  1831. if (IS_ERR(trans)) {
  1832. btrfs_free_path(path);
  1833. return PTR_ERR(trans);
  1834. }
  1835. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1836. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1837. dir_id, "default", 7, 1);
  1838. if (IS_ERR_OR_NULL(di)) {
  1839. btrfs_free_path(path);
  1840. btrfs_end_transaction(trans, root);
  1841. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1842. "this isn't going to work\n");
  1843. return -ENOENT;
  1844. }
  1845. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1846. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1847. btrfs_mark_buffer_dirty(path->nodes[0]);
  1848. btrfs_free_path(path);
  1849. disk_super = &root->fs_info->super_copy;
  1850. features = btrfs_super_incompat_flags(disk_super);
  1851. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1852. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1853. btrfs_set_super_incompat_flags(disk_super, features);
  1854. }
  1855. btrfs_end_transaction(trans, root);
  1856. return 0;
  1857. }
  1858. static void get_block_group_info(struct list_head *groups_list,
  1859. struct btrfs_ioctl_space_info *space)
  1860. {
  1861. struct btrfs_block_group_cache *block_group;
  1862. space->total_bytes = 0;
  1863. space->used_bytes = 0;
  1864. space->flags = 0;
  1865. list_for_each_entry(block_group, groups_list, list) {
  1866. space->flags = block_group->flags;
  1867. space->total_bytes += block_group->key.offset;
  1868. space->used_bytes +=
  1869. btrfs_block_group_used(&block_group->item);
  1870. }
  1871. }
  1872. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1873. {
  1874. struct btrfs_ioctl_space_args space_args;
  1875. struct btrfs_ioctl_space_info space;
  1876. struct btrfs_ioctl_space_info *dest;
  1877. struct btrfs_ioctl_space_info *dest_orig;
  1878. struct btrfs_ioctl_space_info *user_dest;
  1879. struct btrfs_space_info *info;
  1880. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  1881. BTRFS_BLOCK_GROUP_SYSTEM,
  1882. BTRFS_BLOCK_GROUP_METADATA,
  1883. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  1884. int num_types = 4;
  1885. int alloc_size;
  1886. int ret = 0;
  1887. u64 slot_count = 0;
  1888. int i, c;
  1889. if (copy_from_user(&space_args,
  1890. (struct btrfs_ioctl_space_args __user *)arg,
  1891. sizeof(space_args)))
  1892. return -EFAULT;
  1893. for (i = 0; i < num_types; i++) {
  1894. struct btrfs_space_info *tmp;
  1895. info = NULL;
  1896. rcu_read_lock();
  1897. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1898. list) {
  1899. if (tmp->flags == types[i]) {
  1900. info = tmp;
  1901. break;
  1902. }
  1903. }
  1904. rcu_read_unlock();
  1905. if (!info)
  1906. continue;
  1907. down_read(&info->groups_sem);
  1908. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1909. if (!list_empty(&info->block_groups[c]))
  1910. slot_count++;
  1911. }
  1912. up_read(&info->groups_sem);
  1913. }
  1914. /* space_slots == 0 means they are asking for a count */
  1915. if (space_args.space_slots == 0) {
  1916. space_args.total_spaces = slot_count;
  1917. goto out;
  1918. }
  1919. slot_count = min_t(u64, space_args.space_slots, slot_count);
  1920. alloc_size = sizeof(*dest) * slot_count;
  1921. /* we generally have at most 6 or so space infos, one for each raid
  1922. * level. So, a whole page should be more than enough for everyone
  1923. */
  1924. if (alloc_size > PAGE_CACHE_SIZE)
  1925. return -ENOMEM;
  1926. space_args.total_spaces = 0;
  1927. dest = kmalloc(alloc_size, GFP_NOFS);
  1928. if (!dest)
  1929. return -ENOMEM;
  1930. dest_orig = dest;
  1931. /* now we have a buffer to copy into */
  1932. for (i = 0; i < num_types; i++) {
  1933. struct btrfs_space_info *tmp;
  1934. if (!slot_count)
  1935. break;
  1936. info = NULL;
  1937. rcu_read_lock();
  1938. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1939. list) {
  1940. if (tmp->flags == types[i]) {
  1941. info = tmp;
  1942. break;
  1943. }
  1944. }
  1945. rcu_read_unlock();
  1946. if (!info)
  1947. continue;
  1948. down_read(&info->groups_sem);
  1949. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1950. if (!list_empty(&info->block_groups[c])) {
  1951. get_block_group_info(&info->block_groups[c],
  1952. &space);
  1953. memcpy(dest, &space, sizeof(space));
  1954. dest++;
  1955. space_args.total_spaces++;
  1956. slot_count--;
  1957. }
  1958. if (!slot_count)
  1959. break;
  1960. }
  1961. up_read(&info->groups_sem);
  1962. }
  1963. user_dest = (struct btrfs_ioctl_space_info *)
  1964. (arg + sizeof(struct btrfs_ioctl_space_args));
  1965. if (copy_to_user(user_dest, dest_orig, alloc_size))
  1966. ret = -EFAULT;
  1967. kfree(dest_orig);
  1968. out:
  1969. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  1970. ret = -EFAULT;
  1971. return ret;
  1972. }
  1973. /*
  1974. * there are many ways the trans_start and trans_end ioctls can lead
  1975. * to deadlocks. They should only be used by applications that
  1976. * basically own the machine, and have a very in depth understanding
  1977. * of all the possible deadlocks and enospc problems.
  1978. */
  1979. long btrfs_ioctl_trans_end(struct file *file)
  1980. {
  1981. struct inode *inode = fdentry(file)->d_inode;
  1982. struct btrfs_root *root = BTRFS_I(inode)->root;
  1983. struct btrfs_trans_handle *trans;
  1984. trans = file->private_data;
  1985. if (!trans)
  1986. return -EINVAL;
  1987. file->private_data = NULL;
  1988. btrfs_end_transaction(trans, root);
  1989. mutex_lock(&root->fs_info->trans_mutex);
  1990. root->fs_info->open_ioctl_trans--;
  1991. mutex_unlock(&root->fs_info->trans_mutex);
  1992. mnt_drop_write(file->f_path.mnt);
  1993. return 0;
  1994. }
  1995. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  1996. {
  1997. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1998. struct btrfs_trans_handle *trans;
  1999. u64 transid;
  2000. trans = btrfs_start_transaction(root, 0);
  2001. if (IS_ERR(trans))
  2002. return PTR_ERR(trans);
  2003. transid = trans->transid;
  2004. btrfs_commit_transaction_async(trans, root, 0);
  2005. if (argp)
  2006. if (copy_to_user(argp, &transid, sizeof(transid)))
  2007. return -EFAULT;
  2008. return 0;
  2009. }
  2010. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  2011. {
  2012. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2013. u64 transid;
  2014. if (argp) {
  2015. if (copy_from_user(&transid, argp, sizeof(transid)))
  2016. return -EFAULT;
  2017. } else {
  2018. transid = 0; /* current trans */
  2019. }
  2020. return btrfs_wait_for_commit(root, transid);
  2021. }
  2022. long btrfs_ioctl(struct file *file, unsigned int
  2023. cmd, unsigned long arg)
  2024. {
  2025. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2026. void __user *argp = (void __user *)arg;
  2027. switch (cmd) {
  2028. case FS_IOC_GETFLAGS:
  2029. return btrfs_ioctl_getflags(file, argp);
  2030. case FS_IOC_SETFLAGS:
  2031. return btrfs_ioctl_setflags(file, argp);
  2032. case FS_IOC_GETVERSION:
  2033. return btrfs_ioctl_getversion(file, argp);
  2034. case BTRFS_IOC_SNAP_CREATE:
  2035. return btrfs_ioctl_snap_create(file, argp, 0);
  2036. case BTRFS_IOC_SNAP_CREATE_V2:
  2037. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  2038. case BTRFS_IOC_SUBVOL_CREATE:
  2039. return btrfs_ioctl_snap_create(file, argp, 1);
  2040. case BTRFS_IOC_SNAP_DESTROY:
  2041. return btrfs_ioctl_snap_destroy(file, argp);
  2042. case BTRFS_IOC_SUBVOL_GETFLAGS:
  2043. return btrfs_ioctl_subvol_getflags(file, argp);
  2044. case BTRFS_IOC_SUBVOL_SETFLAGS:
  2045. return btrfs_ioctl_subvol_setflags(file, argp);
  2046. case BTRFS_IOC_DEFAULT_SUBVOL:
  2047. return btrfs_ioctl_default_subvol(file, argp);
  2048. case BTRFS_IOC_DEFRAG:
  2049. return btrfs_ioctl_defrag(file, NULL);
  2050. case BTRFS_IOC_DEFRAG_RANGE:
  2051. return btrfs_ioctl_defrag(file, argp);
  2052. case BTRFS_IOC_RESIZE:
  2053. return btrfs_ioctl_resize(root, argp);
  2054. case BTRFS_IOC_ADD_DEV:
  2055. return btrfs_ioctl_add_dev(root, argp);
  2056. case BTRFS_IOC_RM_DEV:
  2057. return btrfs_ioctl_rm_dev(root, argp);
  2058. case BTRFS_IOC_BALANCE:
  2059. return btrfs_balance(root->fs_info->dev_root);
  2060. case BTRFS_IOC_CLONE:
  2061. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  2062. case BTRFS_IOC_CLONE_RANGE:
  2063. return btrfs_ioctl_clone_range(file, argp);
  2064. case BTRFS_IOC_TRANS_START:
  2065. return btrfs_ioctl_trans_start(file);
  2066. case BTRFS_IOC_TRANS_END:
  2067. return btrfs_ioctl_trans_end(file);
  2068. case BTRFS_IOC_TREE_SEARCH:
  2069. return btrfs_ioctl_tree_search(file, argp);
  2070. case BTRFS_IOC_INO_LOOKUP:
  2071. return btrfs_ioctl_ino_lookup(file, argp);
  2072. case BTRFS_IOC_SPACE_INFO:
  2073. return btrfs_ioctl_space_info(root, argp);
  2074. case BTRFS_IOC_SYNC:
  2075. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  2076. return 0;
  2077. case BTRFS_IOC_START_SYNC:
  2078. return btrfs_ioctl_start_sync(file, argp);
  2079. case BTRFS_IOC_WAIT_SYNC:
  2080. return btrfs_ioctl_wait_sync(file, argp);
  2081. }
  2082. return -ENOTTY;
  2083. }