ioctl.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  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(!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. unsigned long last_index;
  555. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  556. unsigned long total_read = 0;
  557. u64 page_start;
  558. u64 page_end;
  559. u64 last_len = 0;
  560. u64 skip = 0;
  561. u64 defrag_end = 0;
  562. unsigned long i;
  563. int ret;
  564. if (inode->i_size == 0)
  565. return 0;
  566. if (range->start + range->len > range->start) {
  567. last_index = min_t(u64, inode->i_size - 1,
  568. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  569. } else {
  570. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  571. }
  572. i = range->start >> PAGE_CACHE_SHIFT;
  573. while (i <= last_index) {
  574. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  575. PAGE_CACHE_SIZE,
  576. range->extent_thresh,
  577. &last_len, &skip,
  578. &defrag_end)) {
  579. unsigned long next;
  580. /*
  581. * the should_defrag function tells us how much to skip
  582. * bump our counter by the suggested amount
  583. */
  584. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  585. i = max(i + 1, next);
  586. continue;
  587. }
  588. if (total_read % ra_pages == 0) {
  589. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  590. min(last_index, i + ra_pages - 1));
  591. }
  592. total_read++;
  593. mutex_lock(&inode->i_mutex);
  594. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  595. BTRFS_I(inode)->force_compress = 1;
  596. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  597. if (ret)
  598. goto err_unlock;
  599. again:
  600. if (inode->i_size == 0 ||
  601. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  602. ret = 0;
  603. goto err_reservations;
  604. }
  605. page = grab_cache_page(inode->i_mapping, i);
  606. if (!page) {
  607. ret = -ENOMEM;
  608. goto err_reservations;
  609. }
  610. if (!PageUptodate(page)) {
  611. btrfs_readpage(NULL, page);
  612. lock_page(page);
  613. if (!PageUptodate(page)) {
  614. unlock_page(page);
  615. page_cache_release(page);
  616. ret = -EIO;
  617. goto err_reservations;
  618. }
  619. }
  620. if (page->mapping != inode->i_mapping) {
  621. unlock_page(page);
  622. page_cache_release(page);
  623. goto again;
  624. }
  625. wait_on_page_writeback(page);
  626. if (PageDirty(page)) {
  627. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  628. goto loop_unlock;
  629. }
  630. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  631. page_end = page_start + PAGE_CACHE_SIZE - 1;
  632. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  633. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  634. if (ordered) {
  635. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  636. unlock_page(page);
  637. page_cache_release(page);
  638. btrfs_start_ordered_extent(inode, ordered, 1);
  639. btrfs_put_ordered_extent(ordered);
  640. goto again;
  641. }
  642. set_page_extent_mapped(page);
  643. /*
  644. * this makes sure page_mkwrite is called on the
  645. * page if it is dirtied again later
  646. */
  647. clear_page_dirty_for_io(page);
  648. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  649. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  650. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  651. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
  652. ClearPageChecked(page);
  653. set_page_dirty(page);
  654. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  655. loop_unlock:
  656. unlock_page(page);
  657. page_cache_release(page);
  658. mutex_unlock(&inode->i_mutex);
  659. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  660. i++;
  661. }
  662. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  663. filemap_flush(inode->i_mapping);
  664. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  665. /* the filemap_flush will queue IO into the worker threads, but
  666. * we have to make sure the IO is actually started and that
  667. * ordered extents get created before we return
  668. */
  669. atomic_inc(&root->fs_info->async_submit_draining);
  670. while (atomic_read(&root->fs_info->nr_async_submits) ||
  671. atomic_read(&root->fs_info->async_delalloc_pages)) {
  672. wait_event(root->fs_info->async_submit_wait,
  673. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  674. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  675. }
  676. atomic_dec(&root->fs_info->async_submit_draining);
  677. mutex_lock(&inode->i_mutex);
  678. BTRFS_I(inode)->force_compress = 0;
  679. mutex_unlock(&inode->i_mutex);
  680. }
  681. return 0;
  682. err_reservations:
  683. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  684. err_unlock:
  685. mutex_unlock(&inode->i_mutex);
  686. return ret;
  687. }
  688. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  689. void __user *arg)
  690. {
  691. u64 new_size;
  692. u64 old_size;
  693. u64 devid = 1;
  694. struct btrfs_ioctl_vol_args *vol_args;
  695. struct btrfs_trans_handle *trans;
  696. struct btrfs_device *device = NULL;
  697. char *sizestr;
  698. char *devstr = NULL;
  699. int ret = 0;
  700. int mod = 0;
  701. if (root->fs_info->sb->s_flags & MS_RDONLY)
  702. return -EROFS;
  703. if (!capable(CAP_SYS_ADMIN))
  704. return -EPERM;
  705. vol_args = memdup_user(arg, sizeof(*vol_args));
  706. if (IS_ERR(vol_args))
  707. return PTR_ERR(vol_args);
  708. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  709. mutex_lock(&root->fs_info->volume_mutex);
  710. sizestr = vol_args->name;
  711. devstr = strchr(sizestr, ':');
  712. if (devstr) {
  713. char *end;
  714. sizestr = devstr + 1;
  715. *devstr = '\0';
  716. devstr = vol_args->name;
  717. devid = simple_strtoull(devstr, &end, 10);
  718. printk(KERN_INFO "resizing devid %llu\n",
  719. (unsigned long long)devid);
  720. }
  721. device = btrfs_find_device(root, devid, NULL, NULL);
  722. if (!device) {
  723. printk(KERN_INFO "resizer unable to find device %llu\n",
  724. (unsigned long long)devid);
  725. ret = -EINVAL;
  726. goto out_unlock;
  727. }
  728. if (!strcmp(sizestr, "max"))
  729. new_size = device->bdev->bd_inode->i_size;
  730. else {
  731. if (sizestr[0] == '-') {
  732. mod = -1;
  733. sizestr++;
  734. } else if (sizestr[0] == '+') {
  735. mod = 1;
  736. sizestr++;
  737. }
  738. new_size = memparse(sizestr, NULL);
  739. if (new_size == 0) {
  740. ret = -EINVAL;
  741. goto out_unlock;
  742. }
  743. }
  744. old_size = device->total_bytes;
  745. if (mod < 0) {
  746. if (new_size > old_size) {
  747. ret = -EINVAL;
  748. goto out_unlock;
  749. }
  750. new_size = old_size - new_size;
  751. } else if (mod > 0) {
  752. new_size = old_size + new_size;
  753. }
  754. if (new_size < 256 * 1024 * 1024) {
  755. ret = -EINVAL;
  756. goto out_unlock;
  757. }
  758. if (new_size > device->bdev->bd_inode->i_size) {
  759. ret = -EFBIG;
  760. goto out_unlock;
  761. }
  762. do_div(new_size, root->sectorsize);
  763. new_size *= root->sectorsize;
  764. printk(KERN_INFO "new size for %s is %llu\n",
  765. device->name, (unsigned long long)new_size);
  766. if (new_size > old_size) {
  767. trans = btrfs_start_transaction(root, 0);
  768. ret = btrfs_grow_device(trans, device, new_size);
  769. btrfs_commit_transaction(trans, root);
  770. } else {
  771. ret = btrfs_shrink_device(device, new_size);
  772. }
  773. out_unlock:
  774. mutex_unlock(&root->fs_info->volume_mutex);
  775. kfree(vol_args);
  776. return ret;
  777. }
  778. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  779. char *name,
  780. unsigned long fd,
  781. int subvol,
  782. u64 *transid,
  783. bool readonly)
  784. {
  785. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  786. struct file *src_file;
  787. int namelen;
  788. int ret = 0;
  789. if (root->fs_info->sb->s_flags & MS_RDONLY)
  790. return -EROFS;
  791. namelen = strlen(name);
  792. if (strchr(name, '/')) {
  793. ret = -EINVAL;
  794. goto out;
  795. }
  796. if (subvol) {
  797. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  798. NULL, transid, readonly);
  799. } else {
  800. struct inode *src_inode;
  801. src_file = fget(fd);
  802. if (!src_file) {
  803. ret = -EINVAL;
  804. goto out;
  805. }
  806. src_inode = src_file->f_path.dentry->d_inode;
  807. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  808. printk(KERN_INFO "btrfs: Snapshot src from "
  809. "another FS\n");
  810. ret = -EINVAL;
  811. fput(src_file);
  812. goto out;
  813. }
  814. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  815. BTRFS_I(src_inode)->root,
  816. transid, readonly);
  817. fput(src_file);
  818. }
  819. out:
  820. return ret;
  821. }
  822. static noinline int btrfs_ioctl_snap_create(struct file *file,
  823. void __user *arg, int subvol)
  824. {
  825. struct btrfs_ioctl_vol_args *vol_args;
  826. int ret;
  827. vol_args = memdup_user(arg, sizeof(*vol_args));
  828. if (IS_ERR(vol_args))
  829. return PTR_ERR(vol_args);
  830. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  831. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  832. vol_args->fd, subvol,
  833. NULL, false);
  834. kfree(vol_args);
  835. return ret;
  836. }
  837. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  838. void __user *arg, int subvol)
  839. {
  840. struct btrfs_ioctl_vol_args_v2 *vol_args;
  841. int ret;
  842. u64 transid = 0;
  843. u64 *ptr = NULL;
  844. bool readonly = false;
  845. vol_args = memdup_user(arg, sizeof(*vol_args));
  846. if (IS_ERR(vol_args))
  847. return PTR_ERR(vol_args);
  848. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  849. if (vol_args->flags &
  850. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  851. ret = -EOPNOTSUPP;
  852. goto out;
  853. }
  854. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  855. ptr = &transid;
  856. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  857. readonly = true;
  858. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  859. vol_args->fd, subvol,
  860. ptr, readonly);
  861. if (ret == 0 && ptr &&
  862. copy_to_user(arg +
  863. offsetof(struct btrfs_ioctl_vol_args_v2,
  864. transid), ptr, sizeof(*ptr)))
  865. ret = -EFAULT;
  866. out:
  867. kfree(vol_args);
  868. return ret;
  869. }
  870. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  871. void __user *arg)
  872. {
  873. struct inode *inode = fdentry(file)->d_inode;
  874. struct btrfs_root *root = BTRFS_I(inode)->root;
  875. int ret = 0;
  876. u64 flags = 0;
  877. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  878. return -EINVAL;
  879. down_read(&root->fs_info->subvol_sem);
  880. if (btrfs_root_readonly(root))
  881. flags |= BTRFS_SUBVOL_RDONLY;
  882. up_read(&root->fs_info->subvol_sem);
  883. if (copy_to_user(arg, &flags, sizeof(flags)))
  884. ret = -EFAULT;
  885. return ret;
  886. }
  887. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  888. void __user *arg)
  889. {
  890. struct inode *inode = fdentry(file)->d_inode;
  891. struct btrfs_root *root = BTRFS_I(inode)->root;
  892. struct btrfs_trans_handle *trans;
  893. u64 root_flags;
  894. u64 flags;
  895. int ret = 0;
  896. if (root->fs_info->sb->s_flags & MS_RDONLY)
  897. return -EROFS;
  898. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  899. return -EINVAL;
  900. if (copy_from_user(&flags, arg, sizeof(flags)))
  901. return -EFAULT;
  902. if (flags & ~BTRFS_SUBVOL_CREATE_ASYNC)
  903. return -EINVAL;
  904. if (flags & ~BTRFS_SUBVOL_RDONLY)
  905. return -EOPNOTSUPP;
  906. down_write(&root->fs_info->subvol_sem);
  907. /* nothing to do */
  908. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  909. goto out;
  910. root_flags = btrfs_root_flags(&root->root_item);
  911. if (flags & BTRFS_SUBVOL_RDONLY)
  912. btrfs_set_root_flags(&root->root_item,
  913. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  914. else
  915. btrfs_set_root_flags(&root->root_item,
  916. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  917. trans = btrfs_start_transaction(root, 1);
  918. if (IS_ERR(trans)) {
  919. ret = PTR_ERR(trans);
  920. goto out_reset;
  921. }
  922. ret = btrfs_update_root(trans, root,
  923. &root->root_key, &root->root_item);
  924. btrfs_commit_transaction(trans, root);
  925. out_reset:
  926. if (ret)
  927. btrfs_set_root_flags(&root->root_item, root_flags);
  928. out:
  929. up_write(&root->fs_info->subvol_sem);
  930. return ret;
  931. }
  932. /*
  933. * helper to check if the subvolume references other subvolumes
  934. */
  935. static noinline int may_destroy_subvol(struct btrfs_root *root)
  936. {
  937. struct btrfs_path *path;
  938. struct btrfs_key key;
  939. int ret;
  940. path = btrfs_alloc_path();
  941. if (!path)
  942. return -ENOMEM;
  943. key.objectid = root->root_key.objectid;
  944. key.type = BTRFS_ROOT_REF_KEY;
  945. key.offset = (u64)-1;
  946. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  947. &key, path, 0, 0);
  948. if (ret < 0)
  949. goto out;
  950. BUG_ON(ret == 0);
  951. ret = 0;
  952. if (path->slots[0] > 0) {
  953. path->slots[0]--;
  954. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  955. if (key.objectid == root->root_key.objectid &&
  956. key.type == BTRFS_ROOT_REF_KEY)
  957. ret = -ENOTEMPTY;
  958. }
  959. out:
  960. btrfs_free_path(path);
  961. return ret;
  962. }
  963. static noinline int key_in_sk(struct btrfs_key *key,
  964. struct btrfs_ioctl_search_key *sk)
  965. {
  966. struct btrfs_key test;
  967. int ret;
  968. test.objectid = sk->min_objectid;
  969. test.type = sk->min_type;
  970. test.offset = sk->min_offset;
  971. ret = btrfs_comp_cpu_keys(key, &test);
  972. if (ret < 0)
  973. return 0;
  974. test.objectid = sk->max_objectid;
  975. test.type = sk->max_type;
  976. test.offset = sk->max_offset;
  977. ret = btrfs_comp_cpu_keys(key, &test);
  978. if (ret > 0)
  979. return 0;
  980. return 1;
  981. }
  982. static noinline int copy_to_sk(struct btrfs_root *root,
  983. struct btrfs_path *path,
  984. struct btrfs_key *key,
  985. struct btrfs_ioctl_search_key *sk,
  986. char *buf,
  987. unsigned long *sk_offset,
  988. int *num_found)
  989. {
  990. u64 found_transid;
  991. struct extent_buffer *leaf;
  992. struct btrfs_ioctl_search_header sh;
  993. unsigned long item_off;
  994. unsigned long item_len;
  995. int nritems;
  996. int i;
  997. int slot;
  998. int found = 0;
  999. int ret = 0;
  1000. leaf = path->nodes[0];
  1001. slot = path->slots[0];
  1002. nritems = btrfs_header_nritems(leaf);
  1003. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1004. i = nritems;
  1005. goto advance_key;
  1006. }
  1007. found_transid = btrfs_header_generation(leaf);
  1008. for (i = slot; i < nritems; i++) {
  1009. item_off = btrfs_item_ptr_offset(leaf, i);
  1010. item_len = btrfs_item_size_nr(leaf, i);
  1011. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1012. item_len = 0;
  1013. if (sizeof(sh) + item_len + *sk_offset >
  1014. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1015. ret = 1;
  1016. goto overflow;
  1017. }
  1018. btrfs_item_key_to_cpu(leaf, key, i);
  1019. if (!key_in_sk(key, sk))
  1020. continue;
  1021. sh.objectid = key->objectid;
  1022. sh.offset = key->offset;
  1023. sh.type = key->type;
  1024. sh.len = item_len;
  1025. sh.transid = found_transid;
  1026. /* copy search result header */
  1027. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1028. *sk_offset += sizeof(sh);
  1029. if (item_len) {
  1030. char *p = buf + *sk_offset;
  1031. /* copy the item */
  1032. read_extent_buffer(leaf, p,
  1033. item_off, item_len);
  1034. *sk_offset += item_len;
  1035. }
  1036. found++;
  1037. if (*num_found >= sk->nr_items)
  1038. break;
  1039. }
  1040. advance_key:
  1041. ret = 0;
  1042. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1043. key->offset++;
  1044. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1045. key->offset = 0;
  1046. key->type++;
  1047. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1048. key->offset = 0;
  1049. key->type = 0;
  1050. key->objectid++;
  1051. } else
  1052. ret = 1;
  1053. overflow:
  1054. *num_found += found;
  1055. return ret;
  1056. }
  1057. static noinline int search_ioctl(struct inode *inode,
  1058. struct btrfs_ioctl_search_args *args)
  1059. {
  1060. struct btrfs_root *root;
  1061. struct btrfs_key key;
  1062. struct btrfs_key max_key;
  1063. struct btrfs_path *path;
  1064. struct btrfs_ioctl_search_key *sk = &args->key;
  1065. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1066. int ret;
  1067. int num_found = 0;
  1068. unsigned long sk_offset = 0;
  1069. path = btrfs_alloc_path();
  1070. if (!path)
  1071. return -ENOMEM;
  1072. if (sk->tree_id == 0) {
  1073. /* search the root of the inode that was passed */
  1074. root = BTRFS_I(inode)->root;
  1075. } else {
  1076. key.objectid = sk->tree_id;
  1077. key.type = BTRFS_ROOT_ITEM_KEY;
  1078. key.offset = (u64)-1;
  1079. root = btrfs_read_fs_root_no_name(info, &key);
  1080. if (IS_ERR(root)) {
  1081. printk(KERN_ERR "could not find root %llu\n",
  1082. sk->tree_id);
  1083. btrfs_free_path(path);
  1084. return -ENOENT;
  1085. }
  1086. }
  1087. key.objectid = sk->min_objectid;
  1088. key.type = sk->min_type;
  1089. key.offset = sk->min_offset;
  1090. max_key.objectid = sk->max_objectid;
  1091. max_key.type = sk->max_type;
  1092. max_key.offset = sk->max_offset;
  1093. path->keep_locks = 1;
  1094. while(1) {
  1095. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1096. sk->min_transid);
  1097. if (ret != 0) {
  1098. if (ret > 0)
  1099. ret = 0;
  1100. goto err;
  1101. }
  1102. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1103. &sk_offset, &num_found);
  1104. btrfs_release_path(root, path);
  1105. if (ret || num_found >= sk->nr_items)
  1106. break;
  1107. }
  1108. ret = 0;
  1109. err:
  1110. sk->nr_items = num_found;
  1111. btrfs_free_path(path);
  1112. return ret;
  1113. }
  1114. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1115. void __user *argp)
  1116. {
  1117. struct btrfs_ioctl_search_args *args;
  1118. struct inode *inode;
  1119. int ret;
  1120. if (!capable(CAP_SYS_ADMIN))
  1121. return -EPERM;
  1122. args = memdup_user(argp, sizeof(*args));
  1123. if (IS_ERR(args))
  1124. return PTR_ERR(args);
  1125. inode = fdentry(file)->d_inode;
  1126. ret = search_ioctl(inode, args);
  1127. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1128. ret = -EFAULT;
  1129. kfree(args);
  1130. return ret;
  1131. }
  1132. /*
  1133. * Search INODE_REFs to identify path name of 'dirid' directory
  1134. * in a 'tree_id' tree. and sets path name to 'name'.
  1135. */
  1136. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1137. u64 tree_id, u64 dirid, char *name)
  1138. {
  1139. struct btrfs_root *root;
  1140. struct btrfs_key key;
  1141. char *ptr;
  1142. int ret = -1;
  1143. int slot;
  1144. int len;
  1145. int total_len = 0;
  1146. struct btrfs_inode_ref *iref;
  1147. struct extent_buffer *l;
  1148. struct btrfs_path *path;
  1149. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1150. name[0]='\0';
  1151. return 0;
  1152. }
  1153. path = btrfs_alloc_path();
  1154. if (!path)
  1155. return -ENOMEM;
  1156. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1157. key.objectid = tree_id;
  1158. key.type = BTRFS_ROOT_ITEM_KEY;
  1159. key.offset = (u64)-1;
  1160. root = btrfs_read_fs_root_no_name(info, &key);
  1161. if (IS_ERR(root)) {
  1162. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1163. ret = -ENOENT;
  1164. goto out;
  1165. }
  1166. key.objectid = dirid;
  1167. key.type = BTRFS_INODE_REF_KEY;
  1168. key.offset = (u64)-1;
  1169. while(1) {
  1170. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1171. if (ret < 0)
  1172. goto out;
  1173. l = path->nodes[0];
  1174. slot = path->slots[0];
  1175. if (ret > 0 && slot > 0)
  1176. slot--;
  1177. btrfs_item_key_to_cpu(l, &key, slot);
  1178. if (ret > 0 && (key.objectid != dirid ||
  1179. key.type != BTRFS_INODE_REF_KEY)) {
  1180. ret = -ENOENT;
  1181. goto out;
  1182. }
  1183. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1184. len = btrfs_inode_ref_name_len(l, iref);
  1185. ptr -= len + 1;
  1186. total_len += len + 1;
  1187. if (ptr < name)
  1188. goto out;
  1189. *(ptr + len) = '/';
  1190. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1191. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1192. break;
  1193. btrfs_release_path(root, path);
  1194. key.objectid = key.offset;
  1195. key.offset = (u64)-1;
  1196. dirid = key.objectid;
  1197. }
  1198. if (ptr < name)
  1199. goto out;
  1200. memcpy(name, ptr, total_len);
  1201. name[total_len]='\0';
  1202. ret = 0;
  1203. out:
  1204. btrfs_free_path(path);
  1205. return ret;
  1206. }
  1207. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1208. void __user *argp)
  1209. {
  1210. struct btrfs_ioctl_ino_lookup_args *args;
  1211. struct inode *inode;
  1212. int ret;
  1213. if (!capable(CAP_SYS_ADMIN))
  1214. return -EPERM;
  1215. args = memdup_user(argp, sizeof(*args));
  1216. if (IS_ERR(args))
  1217. return PTR_ERR(args);
  1218. inode = fdentry(file)->d_inode;
  1219. if (args->treeid == 0)
  1220. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1221. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1222. args->treeid, args->objectid,
  1223. args->name);
  1224. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1225. ret = -EFAULT;
  1226. kfree(args);
  1227. return ret;
  1228. }
  1229. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1230. void __user *arg)
  1231. {
  1232. struct dentry *parent = fdentry(file);
  1233. struct dentry *dentry;
  1234. struct inode *dir = parent->d_inode;
  1235. struct inode *inode;
  1236. struct btrfs_root *root = BTRFS_I(dir)->root;
  1237. struct btrfs_root *dest = NULL;
  1238. struct btrfs_ioctl_vol_args *vol_args;
  1239. struct btrfs_trans_handle *trans;
  1240. int namelen;
  1241. int ret;
  1242. int err = 0;
  1243. vol_args = memdup_user(arg, sizeof(*vol_args));
  1244. if (IS_ERR(vol_args))
  1245. return PTR_ERR(vol_args);
  1246. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1247. namelen = strlen(vol_args->name);
  1248. if (strchr(vol_args->name, '/') ||
  1249. strncmp(vol_args->name, "..", namelen) == 0) {
  1250. err = -EINVAL;
  1251. goto out;
  1252. }
  1253. err = mnt_want_write(file->f_path.mnt);
  1254. if (err)
  1255. goto out;
  1256. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1257. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1258. if (IS_ERR(dentry)) {
  1259. err = PTR_ERR(dentry);
  1260. goto out_unlock_dir;
  1261. }
  1262. if (!dentry->d_inode) {
  1263. err = -ENOENT;
  1264. goto out_dput;
  1265. }
  1266. inode = dentry->d_inode;
  1267. dest = BTRFS_I(inode)->root;
  1268. if (!capable(CAP_SYS_ADMIN)){
  1269. /*
  1270. * Regular user. Only allow this with a special mount
  1271. * option, when the user has write+exec access to the
  1272. * subvol root, and when rmdir(2) would have been
  1273. * allowed.
  1274. *
  1275. * Note that this is _not_ check that the subvol is
  1276. * empty or doesn't contain data that we wouldn't
  1277. * otherwise be able to delete.
  1278. *
  1279. * Users who want to delete empty subvols should try
  1280. * rmdir(2).
  1281. */
  1282. err = -EPERM;
  1283. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1284. goto out_dput;
  1285. /*
  1286. * Do not allow deletion if the parent dir is the same
  1287. * as the dir to be deleted. That means the ioctl
  1288. * must be called on the dentry referencing the root
  1289. * of the subvol, not a random directory contained
  1290. * within it.
  1291. */
  1292. err = -EINVAL;
  1293. if (root == dest)
  1294. goto out_dput;
  1295. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1296. if (err)
  1297. goto out_dput;
  1298. /* check if subvolume may be deleted by a non-root user */
  1299. err = btrfs_may_delete(dir, dentry, 1);
  1300. if (err)
  1301. goto out_dput;
  1302. }
  1303. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1304. err = -EINVAL;
  1305. goto out_dput;
  1306. }
  1307. mutex_lock(&inode->i_mutex);
  1308. err = d_invalidate(dentry);
  1309. if (err)
  1310. goto out_unlock;
  1311. down_write(&root->fs_info->subvol_sem);
  1312. err = may_destroy_subvol(dest);
  1313. if (err)
  1314. goto out_up_write;
  1315. trans = btrfs_start_transaction(root, 0);
  1316. if (IS_ERR(trans)) {
  1317. err = PTR_ERR(trans);
  1318. goto out_up_write;
  1319. }
  1320. trans->block_rsv = &root->fs_info->global_block_rsv;
  1321. ret = btrfs_unlink_subvol(trans, root, dir,
  1322. dest->root_key.objectid,
  1323. dentry->d_name.name,
  1324. dentry->d_name.len);
  1325. BUG_ON(ret);
  1326. btrfs_record_root_in_trans(trans, dest);
  1327. memset(&dest->root_item.drop_progress, 0,
  1328. sizeof(dest->root_item.drop_progress));
  1329. dest->root_item.drop_level = 0;
  1330. btrfs_set_root_refs(&dest->root_item, 0);
  1331. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1332. ret = btrfs_insert_orphan_item(trans,
  1333. root->fs_info->tree_root,
  1334. dest->root_key.objectid);
  1335. BUG_ON(ret);
  1336. }
  1337. ret = btrfs_end_transaction(trans, root);
  1338. BUG_ON(ret);
  1339. inode->i_flags |= S_DEAD;
  1340. out_up_write:
  1341. up_write(&root->fs_info->subvol_sem);
  1342. out_unlock:
  1343. mutex_unlock(&inode->i_mutex);
  1344. if (!err) {
  1345. shrink_dcache_sb(root->fs_info->sb);
  1346. btrfs_invalidate_inodes(dest);
  1347. d_delete(dentry);
  1348. }
  1349. out_dput:
  1350. dput(dentry);
  1351. out_unlock_dir:
  1352. mutex_unlock(&dir->i_mutex);
  1353. mnt_drop_write(file->f_path.mnt);
  1354. out:
  1355. kfree(vol_args);
  1356. return err;
  1357. }
  1358. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1359. {
  1360. struct inode *inode = fdentry(file)->d_inode;
  1361. struct btrfs_root *root = BTRFS_I(inode)->root;
  1362. struct btrfs_ioctl_defrag_range_args *range;
  1363. int ret;
  1364. if (btrfs_root_readonly(root))
  1365. return -EROFS;
  1366. ret = mnt_want_write(file->f_path.mnt);
  1367. if (ret)
  1368. return ret;
  1369. switch (inode->i_mode & S_IFMT) {
  1370. case S_IFDIR:
  1371. if (!capable(CAP_SYS_ADMIN)) {
  1372. ret = -EPERM;
  1373. goto out;
  1374. }
  1375. ret = btrfs_defrag_root(root, 0);
  1376. if (ret)
  1377. goto out;
  1378. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1379. break;
  1380. case S_IFREG:
  1381. if (!(file->f_mode & FMODE_WRITE)) {
  1382. ret = -EINVAL;
  1383. goto out;
  1384. }
  1385. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1386. if (!range) {
  1387. ret = -ENOMEM;
  1388. goto out;
  1389. }
  1390. if (argp) {
  1391. if (copy_from_user(range, argp,
  1392. sizeof(*range))) {
  1393. ret = -EFAULT;
  1394. kfree(range);
  1395. goto out;
  1396. }
  1397. /* compression requires us to start the IO */
  1398. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1399. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1400. range->extent_thresh = (u32)-1;
  1401. }
  1402. } else {
  1403. /* the rest are all set to zero by kzalloc */
  1404. range->len = (u64)-1;
  1405. }
  1406. ret = btrfs_defrag_file(file, range);
  1407. kfree(range);
  1408. break;
  1409. default:
  1410. ret = -EINVAL;
  1411. }
  1412. out:
  1413. mnt_drop_write(file->f_path.mnt);
  1414. return ret;
  1415. }
  1416. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1417. {
  1418. struct btrfs_ioctl_vol_args *vol_args;
  1419. int ret;
  1420. if (!capable(CAP_SYS_ADMIN))
  1421. return -EPERM;
  1422. vol_args = memdup_user(arg, sizeof(*vol_args));
  1423. if (IS_ERR(vol_args))
  1424. return PTR_ERR(vol_args);
  1425. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1426. ret = btrfs_init_new_device(root, vol_args->name);
  1427. kfree(vol_args);
  1428. return ret;
  1429. }
  1430. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1431. {
  1432. struct btrfs_ioctl_vol_args *vol_args;
  1433. int ret;
  1434. if (!capable(CAP_SYS_ADMIN))
  1435. return -EPERM;
  1436. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1437. return -EROFS;
  1438. vol_args = memdup_user(arg, sizeof(*vol_args));
  1439. if (IS_ERR(vol_args))
  1440. return PTR_ERR(vol_args);
  1441. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1442. ret = btrfs_rm_device(root, vol_args->name);
  1443. kfree(vol_args);
  1444. return ret;
  1445. }
  1446. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1447. u64 off, u64 olen, u64 destoff)
  1448. {
  1449. struct inode *inode = fdentry(file)->d_inode;
  1450. struct btrfs_root *root = BTRFS_I(inode)->root;
  1451. struct file *src_file;
  1452. struct inode *src;
  1453. struct btrfs_trans_handle *trans;
  1454. struct btrfs_path *path;
  1455. struct extent_buffer *leaf;
  1456. char *buf;
  1457. struct btrfs_key key;
  1458. u32 nritems;
  1459. int slot;
  1460. int ret;
  1461. u64 len = olen;
  1462. u64 bs = root->fs_info->sb->s_blocksize;
  1463. u64 hint_byte;
  1464. /*
  1465. * TODO:
  1466. * - split compressed inline extents. annoying: we need to
  1467. * decompress into destination's address_space (the file offset
  1468. * may change, so source mapping won't do), then recompress (or
  1469. * otherwise reinsert) a subrange.
  1470. * - allow ranges within the same file to be cloned (provided
  1471. * they don't overlap)?
  1472. */
  1473. /* the destination must be opened for writing */
  1474. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1475. return -EINVAL;
  1476. if (btrfs_root_readonly(root))
  1477. return -EROFS;
  1478. ret = mnt_want_write(file->f_path.mnt);
  1479. if (ret)
  1480. return ret;
  1481. src_file = fget(srcfd);
  1482. if (!src_file) {
  1483. ret = -EBADF;
  1484. goto out_drop_write;
  1485. }
  1486. src = src_file->f_dentry->d_inode;
  1487. ret = -EINVAL;
  1488. if (src == inode)
  1489. goto out_fput;
  1490. /* the src must be open for reading */
  1491. if (!(src_file->f_mode & FMODE_READ))
  1492. goto out_fput;
  1493. ret = -EISDIR;
  1494. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1495. goto out_fput;
  1496. ret = -EXDEV;
  1497. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1498. goto out_fput;
  1499. ret = -ENOMEM;
  1500. buf = vmalloc(btrfs_level_size(root, 0));
  1501. if (!buf)
  1502. goto out_fput;
  1503. path = btrfs_alloc_path();
  1504. if (!path) {
  1505. vfree(buf);
  1506. goto out_fput;
  1507. }
  1508. path->reada = 2;
  1509. if (inode < src) {
  1510. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1511. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1512. } else {
  1513. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1514. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1515. }
  1516. /* determine range to clone */
  1517. ret = -EINVAL;
  1518. if (off + len > src->i_size || off + len < off)
  1519. goto out_unlock;
  1520. if (len == 0)
  1521. olen = len = src->i_size - off;
  1522. /* if we extend to eof, continue to block boundary */
  1523. if (off + len == src->i_size)
  1524. len = ALIGN(src->i_size, bs) - off;
  1525. /* verify the end result is block aligned */
  1526. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  1527. !IS_ALIGNED(destoff, bs))
  1528. goto out_unlock;
  1529. /* do any pending delalloc/csum calc on src, one way or
  1530. another, and lock file content */
  1531. while (1) {
  1532. struct btrfs_ordered_extent *ordered;
  1533. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1534. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1535. if (!ordered &&
  1536. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1537. EXTENT_DELALLOC, 0, NULL))
  1538. break;
  1539. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1540. if (ordered)
  1541. btrfs_put_ordered_extent(ordered);
  1542. btrfs_wait_ordered_range(src, off, len);
  1543. }
  1544. /* clone data */
  1545. key.objectid = src->i_ino;
  1546. key.type = BTRFS_EXTENT_DATA_KEY;
  1547. key.offset = 0;
  1548. while (1) {
  1549. /*
  1550. * note the key will change type as we walk through the
  1551. * tree.
  1552. */
  1553. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1554. if (ret < 0)
  1555. goto out;
  1556. nritems = btrfs_header_nritems(path->nodes[0]);
  1557. if (path->slots[0] >= nritems) {
  1558. ret = btrfs_next_leaf(root, path);
  1559. if (ret < 0)
  1560. goto out;
  1561. if (ret > 0)
  1562. break;
  1563. nritems = btrfs_header_nritems(path->nodes[0]);
  1564. }
  1565. leaf = path->nodes[0];
  1566. slot = path->slots[0];
  1567. btrfs_item_key_to_cpu(leaf, &key, slot);
  1568. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1569. key.objectid != src->i_ino)
  1570. break;
  1571. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1572. struct btrfs_file_extent_item *extent;
  1573. int type;
  1574. u32 size;
  1575. struct btrfs_key new_key;
  1576. u64 disko = 0, diskl = 0;
  1577. u64 datao = 0, datal = 0;
  1578. u8 comp;
  1579. u64 endoff;
  1580. size = btrfs_item_size_nr(leaf, slot);
  1581. read_extent_buffer(leaf, buf,
  1582. btrfs_item_ptr_offset(leaf, slot),
  1583. size);
  1584. extent = btrfs_item_ptr(leaf, slot,
  1585. struct btrfs_file_extent_item);
  1586. comp = btrfs_file_extent_compression(leaf, extent);
  1587. type = btrfs_file_extent_type(leaf, extent);
  1588. if (type == BTRFS_FILE_EXTENT_REG ||
  1589. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1590. disko = btrfs_file_extent_disk_bytenr(leaf,
  1591. extent);
  1592. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1593. extent);
  1594. datao = btrfs_file_extent_offset(leaf, extent);
  1595. datal = btrfs_file_extent_num_bytes(leaf,
  1596. extent);
  1597. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1598. /* take upper bound, may be compressed */
  1599. datal = btrfs_file_extent_ram_bytes(leaf,
  1600. extent);
  1601. }
  1602. btrfs_release_path(root, path);
  1603. if (key.offset + datal <= off ||
  1604. key.offset >= off+len)
  1605. goto next;
  1606. memcpy(&new_key, &key, sizeof(new_key));
  1607. new_key.objectid = inode->i_ino;
  1608. new_key.offset = key.offset + destoff - off;
  1609. trans = btrfs_start_transaction(root, 1);
  1610. if (IS_ERR(trans)) {
  1611. ret = PTR_ERR(trans);
  1612. goto out;
  1613. }
  1614. if (type == BTRFS_FILE_EXTENT_REG ||
  1615. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1616. if (off > key.offset) {
  1617. datao += off - key.offset;
  1618. datal -= off - key.offset;
  1619. }
  1620. if (key.offset + datal > off + len)
  1621. datal = off + len - key.offset;
  1622. ret = btrfs_drop_extents(trans, inode,
  1623. new_key.offset,
  1624. new_key.offset + datal,
  1625. &hint_byte, 1);
  1626. BUG_ON(ret);
  1627. ret = btrfs_insert_empty_item(trans, root, path,
  1628. &new_key, size);
  1629. BUG_ON(ret);
  1630. leaf = path->nodes[0];
  1631. slot = path->slots[0];
  1632. write_extent_buffer(leaf, buf,
  1633. btrfs_item_ptr_offset(leaf, slot),
  1634. size);
  1635. extent = btrfs_item_ptr(leaf, slot,
  1636. struct btrfs_file_extent_item);
  1637. /* disko == 0 means it's a hole */
  1638. if (!disko)
  1639. datao = 0;
  1640. btrfs_set_file_extent_offset(leaf, extent,
  1641. datao);
  1642. btrfs_set_file_extent_num_bytes(leaf, extent,
  1643. datal);
  1644. if (disko) {
  1645. inode_add_bytes(inode, datal);
  1646. ret = btrfs_inc_extent_ref(trans, root,
  1647. disko, diskl, 0,
  1648. root->root_key.objectid,
  1649. inode->i_ino,
  1650. new_key.offset - datao);
  1651. BUG_ON(ret);
  1652. }
  1653. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1654. u64 skip = 0;
  1655. u64 trim = 0;
  1656. if (off > key.offset) {
  1657. skip = off - key.offset;
  1658. new_key.offset += skip;
  1659. }
  1660. if (key.offset + datal > off+len)
  1661. trim = key.offset + datal - (off+len);
  1662. if (comp && (skip || trim)) {
  1663. ret = -EINVAL;
  1664. btrfs_end_transaction(trans, root);
  1665. goto out;
  1666. }
  1667. size -= skip + trim;
  1668. datal -= skip + trim;
  1669. ret = btrfs_drop_extents(trans, inode,
  1670. new_key.offset,
  1671. new_key.offset + datal,
  1672. &hint_byte, 1);
  1673. BUG_ON(ret);
  1674. ret = btrfs_insert_empty_item(trans, root, path,
  1675. &new_key, size);
  1676. BUG_ON(ret);
  1677. if (skip) {
  1678. u32 start =
  1679. btrfs_file_extent_calc_inline_size(0);
  1680. memmove(buf+start, buf+start+skip,
  1681. datal);
  1682. }
  1683. leaf = path->nodes[0];
  1684. slot = path->slots[0];
  1685. write_extent_buffer(leaf, buf,
  1686. btrfs_item_ptr_offset(leaf, slot),
  1687. size);
  1688. inode_add_bytes(inode, datal);
  1689. }
  1690. btrfs_mark_buffer_dirty(leaf);
  1691. btrfs_release_path(root, path);
  1692. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1693. /*
  1694. * we round up to the block size at eof when
  1695. * determining which extents to clone above,
  1696. * but shouldn't round up the file size
  1697. */
  1698. endoff = new_key.offset + datal;
  1699. if (endoff > destoff+olen)
  1700. endoff = destoff+olen;
  1701. if (endoff > inode->i_size)
  1702. btrfs_i_size_write(inode, endoff);
  1703. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1704. ret = btrfs_update_inode(trans, root, inode);
  1705. BUG_ON(ret);
  1706. btrfs_end_transaction(trans, root);
  1707. }
  1708. next:
  1709. btrfs_release_path(root, path);
  1710. key.offset++;
  1711. }
  1712. ret = 0;
  1713. out:
  1714. btrfs_release_path(root, path);
  1715. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1716. out_unlock:
  1717. mutex_unlock(&src->i_mutex);
  1718. mutex_unlock(&inode->i_mutex);
  1719. vfree(buf);
  1720. btrfs_free_path(path);
  1721. out_fput:
  1722. fput(src_file);
  1723. out_drop_write:
  1724. mnt_drop_write(file->f_path.mnt);
  1725. return ret;
  1726. }
  1727. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1728. {
  1729. struct btrfs_ioctl_clone_range_args args;
  1730. if (copy_from_user(&args, argp, sizeof(args)))
  1731. return -EFAULT;
  1732. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1733. args.src_length, args.dest_offset);
  1734. }
  1735. /*
  1736. * there are many ways the trans_start and trans_end ioctls can lead
  1737. * to deadlocks. They should only be used by applications that
  1738. * basically own the machine, and have a very in depth understanding
  1739. * of all the possible deadlocks and enospc problems.
  1740. */
  1741. static long btrfs_ioctl_trans_start(struct file *file)
  1742. {
  1743. struct inode *inode = fdentry(file)->d_inode;
  1744. struct btrfs_root *root = BTRFS_I(inode)->root;
  1745. struct btrfs_trans_handle *trans;
  1746. int ret;
  1747. ret = -EPERM;
  1748. if (!capable(CAP_SYS_ADMIN))
  1749. goto out;
  1750. ret = -EINPROGRESS;
  1751. if (file->private_data)
  1752. goto out;
  1753. ret = -EROFS;
  1754. if (btrfs_root_readonly(root))
  1755. goto out;
  1756. ret = mnt_want_write(file->f_path.mnt);
  1757. if (ret)
  1758. goto out;
  1759. mutex_lock(&root->fs_info->trans_mutex);
  1760. root->fs_info->open_ioctl_trans++;
  1761. mutex_unlock(&root->fs_info->trans_mutex);
  1762. ret = -ENOMEM;
  1763. trans = btrfs_start_ioctl_transaction(root, 0);
  1764. if (!trans)
  1765. goto out_drop;
  1766. file->private_data = trans;
  1767. return 0;
  1768. out_drop:
  1769. mutex_lock(&root->fs_info->trans_mutex);
  1770. root->fs_info->open_ioctl_trans--;
  1771. mutex_unlock(&root->fs_info->trans_mutex);
  1772. mnt_drop_write(file->f_path.mnt);
  1773. out:
  1774. return ret;
  1775. }
  1776. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1777. {
  1778. struct inode *inode = fdentry(file)->d_inode;
  1779. struct btrfs_root *root = BTRFS_I(inode)->root;
  1780. struct btrfs_root *new_root;
  1781. struct btrfs_dir_item *di;
  1782. struct btrfs_trans_handle *trans;
  1783. struct btrfs_path *path;
  1784. struct btrfs_key location;
  1785. struct btrfs_disk_key disk_key;
  1786. struct btrfs_super_block *disk_super;
  1787. u64 features;
  1788. u64 objectid = 0;
  1789. u64 dir_id;
  1790. if (!capable(CAP_SYS_ADMIN))
  1791. return -EPERM;
  1792. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1793. return -EFAULT;
  1794. if (!objectid)
  1795. objectid = root->root_key.objectid;
  1796. location.objectid = objectid;
  1797. location.type = BTRFS_ROOT_ITEM_KEY;
  1798. location.offset = (u64)-1;
  1799. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1800. if (IS_ERR(new_root))
  1801. return PTR_ERR(new_root);
  1802. if (btrfs_root_refs(&new_root->root_item) == 0)
  1803. return -ENOENT;
  1804. path = btrfs_alloc_path();
  1805. if (!path)
  1806. return -ENOMEM;
  1807. path->leave_spinning = 1;
  1808. trans = btrfs_start_transaction(root, 1);
  1809. if (!trans) {
  1810. btrfs_free_path(path);
  1811. return -ENOMEM;
  1812. }
  1813. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1814. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1815. dir_id, "default", 7, 1);
  1816. if (IS_ERR_OR_NULL(di)) {
  1817. btrfs_free_path(path);
  1818. btrfs_end_transaction(trans, root);
  1819. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1820. "this isn't going to work\n");
  1821. return -ENOENT;
  1822. }
  1823. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1824. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1825. btrfs_mark_buffer_dirty(path->nodes[0]);
  1826. btrfs_free_path(path);
  1827. disk_super = &root->fs_info->super_copy;
  1828. features = btrfs_super_incompat_flags(disk_super);
  1829. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1830. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1831. btrfs_set_super_incompat_flags(disk_super, features);
  1832. }
  1833. btrfs_end_transaction(trans, root);
  1834. return 0;
  1835. }
  1836. static void get_block_group_info(struct list_head *groups_list,
  1837. struct btrfs_ioctl_space_info *space)
  1838. {
  1839. struct btrfs_block_group_cache *block_group;
  1840. space->total_bytes = 0;
  1841. space->used_bytes = 0;
  1842. space->flags = 0;
  1843. list_for_each_entry(block_group, groups_list, list) {
  1844. space->flags = block_group->flags;
  1845. space->total_bytes += block_group->key.offset;
  1846. space->used_bytes +=
  1847. btrfs_block_group_used(&block_group->item);
  1848. }
  1849. }
  1850. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1851. {
  1852. struct btrfs_ioctl_space_args space_args;
  1853. struct btrfs_ioctl_space_info space;
  1854. struct btrfs_ioctl_space_info *dest;
  1855. struct btrfs_ioctl_space_info *dest_orig;
  1856. struct btrfs_ioctl_space_info *user_dest;
  1857. struct btrfs_space_info *info;
  1858. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  1859. BTRFS_BLOCK_GROUP_SYSTEM,
  1860. BTRFS_BLOCK_GROUP_METADATA,
  1861. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  1862. int num_types = 4;
  1863. int alloc_size;
  1864. int ret = 0;
  1865. int slot_count = 0;
  1866. int i, c;
  1867. if (copy_from_user(&space_args,
  1868. (struct btrfs_ioctl_space_args __user *)arg,
  1869. sizeof(space_args)))
  1870. return -EFAULT;
  1871. for (i = 0; i < num_types; i++) {
  1872. struct btrfs_space_info *tmp;
  1873. info = NULL;
  1874. rcu_read_lock();
  1875. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1876. list) {
  1877. if (tmp->flags == types[i]) {
  1878. info = tmp;
  1879. break;
  1880. }
  1881. }
  1882. rcu_read_unlock();
  1883. if (!info)
  1884. continue;
  1885. down_read(&info->groups_sem);
  1886. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1887. if (!list_empty(&info->block_groups[c]))
  1888. slot_count++;
  1889. }
  1890. up_read(&info->groups_sem);
  1891. }
  1892. /* space_slots == 0 means they are asking for a count */
  1893. if (space_args.space_slots == 0) {
  1894. space_args.total_spaces = slot_count;
  1895. goto out;
  1896. }
  1897. slot_count = min_t(int, space_args.space_slots, slot_count);
  1898. alloc_size = sizeof(*dest) * slot_count;
  1899. /* we generally have at most 6 or so space infos, one for each raid
  1900. * level. So, a whole page should be more than enough for everyone
  1901. */
  1902. if (alloc_size > PAGE_CACHE_SIZE)
  1903. return -ENOMEM;
  1904. space_args.total_spaces = 0;
  1905. dest = kmalloc(alloc_size, GFP_NOFS);
  1906. if (!dest)
  1907. return -ENOMEM;
  1908. dest_orig = dest;
  1909. /* now we have a buffer to copy into */
  1910. for (i = 0; i < num_types; i++) {
  1911. struct btrfs_space_info *tmp;
  1912. info = NULL;
  1913. rcu_read_lock();
  1914. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1915. list) {
  1916. if (tmp->flags == types[i]) {
  1917. info = tmp;
  1918. break;
  1919. }
  1920. }
  1921. rcu_read_unlock();
  1922. if (!info)
  1923. continue;
  1924. down_read(&info->groups_sem);
  1925. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1926. if (!list_empty(&info->block_groups[c])) {
  1927. get_block_group_info(&info->block_groups[c],
  1928. &space);
  1929. memcpy(dest, &space, sizeof(space));
  1930. dest++;
  1931. space_args.total_spaces++;
  1932. }
  1933. }
  1934. up_read(&info->groups_sem);
  1935. }
  1936. user_dest = (struct btrfs_ioctl_space_info *)
  1937. (arg + sizeof(struct btrfs_ioctl_space_args));
  1938. if (copy_to_user(user_dest, dest_orig, alloc_size))
  1939. ret = -EFAULT;
  1940. kfree(dest_orig);
  1941. out:
  1942. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  1943. ret = -EFAULT;
  1944. return ret;
  1945. }
  1946. /*
  1947. * there are many ways the trans_start and trans_end ioctls can lead
  1948. * to deadlocks. They should only be used by applications that
  1949. * basically own the machine, and have a very in depth understanding
  1950. * of all the possible deadlocks and enospc problems.
  1951. */
  1952. long btrfs_ioctl_trans_end(struct file *file)
  1953. {
  1954. struct inode *inode = fdentry(file)->d_inode;
  1955. struct btrfs_root *root = BTRFS_I(inode)->root;
  1956. struct btrfs_trans_handle *trans;
  1957. trans = file->private_data;
  1958. if (!trans)
  1959. return -EINVAL;
  1960. file->private_data = NULL;
  1961. btrfs_end_transaction(trans, root);
  1962. mutex_lock(&root->fs_info->trans_mutex);
  1963. root->fs_info->open_ioctl_trans--;
  1964. mutex_unlock(&root->fs_info->trans_mutex);
  1965. mnt_drop_write(file->f_path.mnt);
  1966. return 0;
  1967. }
  1968. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  1969. {
  1970. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1971. struct btrfs_trans_handle *trans;
  1972. u64 transid;
  1973. trans = btrfs_start_transaction(root, 0);
  1974. transid = trans->transid;
  1975. btrfs_commit_transaction_async(trans, root, 0);
  1976. if (argp)
  1977. if (copy_to_user(argp, &transid, sizeof(transid)))
  1978. return -EFAULT;
  1979. return 0;
  1980. }
  1981. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  1982. {
  1983. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1984. u64 transid;
  1985. if (argp) {
  1986. if (copy_from_user(&transid, argp, sizeof(transid)))
  1987. return -EFAULT;
  1988. } else {
  1989. transid = 0; /* current trans */
  1990. }
  1991. return btrfs_wait_for_commit(root, transid);
  1992. }
  1993. long btrfs_ioctl(struct file *file, unsigned int
  1994. cmd, unsigned long arg)
  1995. {
  1996. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1997. void __user *argp = (void __user *)arg;
  1998. switch (cmd) {
  1999. case FS_IOC_GETFLAGS:
  2000. return btrfs_ioctl_getflags(file, argp);
  2001. case FS_IOC_SETFLAGS:
  2002. return btrfs_ioctl_setflags(file, argp);
  2003. case FS_IOC_GETVERSION:
  2004. return btrfs_ioctl_getversion(file, argp);
  2005. case BTRFS_IOC_SNAP_CREATE:
  2006. return btrfs_ioctl_snap_create(file, argp, 0);
  2007. case BTRFS_IOC_SNAP_CREATE_V2:
  2008. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  2009. case BTRFS_IOC_SUBVOL_CREATE:
  2010. return btrfs_ioctl_snap_create(file, argp, 1);
  2011. case BTRFS_IOC_SNAP_DESTROY:
  2012. return btrfs_ioctl_snap_destroy(file, argp);
  2013. case BTRFS_IOC_SUBVOL_GETFLAGS:
  2014. return btrfs_ioctl_subvol_getflags(file, argp);
  2015. case BTRFS_IOC_SUBVOL_SETFLAGS:
  2016. return btrfs_ioctl_subvol_setflags(file, argp);
  2017. case BTRFS_IOC_DEFAULT_SUBVOL:
  2018. return btrfs_ioctl_default_subvol(file, argp);
  2019. case BTRFS_IOC_DEFRAG:
  2020. return btrfs_ioctl_defrag(file, NULL);
  2021. case BTRFS_IOC_DEFRAG_RANGE:
  2022. return btrfs_ioctl_defrag(file, argp);
  2023. case BTRFS_IOC_RESIZE:
  2024. return btrfs_ioctl_resize(root, argp);
  2025. case BTRFS_IOC_ADD_DEV:
  2026. return btrfs_ioctl_add_dev(root, argp);
  2027. case BTRFS_IOC_RM_DEV:
  2028. return btrfs_ioctl_rm_dev(root, argp);
  2029. case BTRFS_IOC_BALANCE:
  2030. return btrfs_balance(root->fs_info->dev_root);
  2031. case BTRFS_IOC_CLONE:
  2032. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  2033. case BTRFS_IOC_CLONE_RANGE:
  2034. return btrfs_ioctl_clone_range(file, argp);
  2035. case BTRFS_IOC_TRANS_START:
  2036. return btrfs_ioctl_trans_start(file);
  2037. case BTRFS_IOC_TRANS_END:
  2038. return btrfs_ioctl_trans_end(file);
  2039. case BTRFS_IOC_TREE_SEARCH:
  2040. return btrfs_ioctl_tree_search(file, argp);
  2041. case BTRFS_IOC_INO_LOOKUP:
  2042. return btrfs_ioctl_ino_lookup(file, argp);
  2043. case BTRFS_IOC_SPACE_INFO:
  2044. return btrfs_ioctl_space_info(root, argp);
  2045. case BTRFS_IOC_SYNC:
  2046. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  2047. return 0;
  2048. case BTRFS_IOC_START_SYNC:
  2049. return btrfs_ioctl_start_sync(file, argp);
  2050. case BTRFS_IOC_WAIT_SYNC:
  2051. return btrfs_ioctl_wait_sync(file, argp);
  2052. }
  2053. return -ENOTTY;
  2054. }