inode.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/posix_acl.h>
  15. #include <linux/sort.h>
  16. #include <linux/gfs2_ondisk.h>
  17. #include <linux/crc32.h>
  18. #include "gfs2.h"
  19. #include "lm_interface.h"
  20. #include "incore.h"
  21. #include "acl.h"
  22. #include "bmap.h"
  23. #include "dir.h"
  24. #include "eattr.h"
  25. #include "glock.h"
  26. #include "glops.h"
  27. #include "inode.h"
  28. #include "log.h"
  29. #include "meta_io.h"
  30. #include "ops_address.h"
  31. #include "ops_file.h"
  32. #include "ops_inode.h"
  33. #include "quota.h"
  34. #include "rgrp.h"
  35. #include "trans.h"
  36. #include "unlinked.h"
  37. #include "util.h"
  38. /**
  39. * inode_attr_in - Copy attributes from the dinode into the VFS inode
  40. * @ip: The GFS2 inode (with embedded disk inode data)
  41. * @inode: The Linux VFS inode
  42. *
  43. */
  44. static void inode_attr_in(struct gfs2_inode *ip, struct inode *inode)
  45. {
  46. inode->i_ino = ip->i_num.no_formal_ino;
  47. switch (ip->i_di.di_mode & S_IFMT) {
  48. case S_IFBLK:
  49. case S_IFCHR:
  50. inode->i_rdev = MKDEV(ip->i_di.di_major, ip->i_di.di_minor);
  51. break;
  52. default:
  53. inode->i_rdev = 0;
  54. break;
  55. };
  56. inode->i_mode = ip->i_di.di_mode;
  57. inode->i_nlink = ip->i_di.di_nlink;
  58. inode->i_uid = ip->i_di.di_uid;
  59. inode->i_gid = ip->i_di.di_gid;
  60. i_size_write(inode, ip->i_di.di_size);
  61. inode->i_atime.tv_sec = ip->i_di.di_atime;
  62. inode->i_mtime.tv_sec = ip->i_di.di_mtime;
  63. inode->i_ctime.tv_sec = ip->i_di.di_ctime;
  64. inode->i_atime.tv_nsec = 0;
  65. inode->i_mtime.tv_nsec = 0;
  66. inode->i_ctime.tv_nsec = 0;
  67. inode->i_blksize = PAGE_SIZE;
  68. inode->i_blocks = ip->i_di.di_blocks <<
  69. (ip->i_sbd->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
  70. if (ip->i_di.di_flags & GFS2_DIF_IMMUTABLE)
  71. inode->i_flags |= S_IMMUTABLE;
  72. else
  73. inode->i_flags &= ~S_IMMUTABLE;
  74. if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY)
  75. inode->i_flags |= S_APPEND;
  76. else
  77. inode->i_flags &= ~S_APPEND;
  78. }
  79. /**
  80. * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode
  81. * @ip: The GFS2 inode (with embedded disk inode data)
  82. *
  83. */
  84. void gfs2_inode_attr_in(struct gfs2_inode *ip)
  85. {
  86. struct inode *inode;
  87. inode = gfs2_ip2v_lookup(ip);
  88. if (inode) {
  89. inode_attr_in(ip, inode);
  90. iput(inode);
  91. }
  92. }
  93. /**
  94. * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
  95. * @ip: The GFS2 inode
  96. *
  97. * Only copy out the attributes that we want the VFS layer
  98. * to be able to modify.
  99. */
  100. void gfs2_inode_attr_out(struct gfs2_inode *ip)
  101. {
  102. struct inode *inode = ip->i_vnode;
  103. gfs2_assert_withdraw(ip->i_sbd,
  104. (ip->i_di.di_mode & S_IFMT) == (inode->i_mode & S_IFMT));
  105. ip->i_di.di_mode = inode->i_mode;
  106. ip->i_di.di_uid = inode->i_uid;
  107. ip->i_di.di_gid = inode->i_gid;
  108. ip->i_di.di_atime = inode->i_atime.tv_sec;
  109. ip->i_di.di_mtime = inode->i_mtime.tv_sec;
  110. ip->i_di.di_ctime = inode->i_ctime.tv_sec;
  111. }
  112. /**
  113. * gfs2_ip2v_lookup - Get the struct inode for a struct gfs2_inode
  114. * @ip: the struct gfs2_inode to get the struct inode for
  115. *
  116. * Returns: A VFS inode, or NULL if none
  117. */
  118. struct inode *gfs2_ip2v_lookup(struct gfs2_inode *ip)
  119. {
  120. struct inode *inode = NULL;
  121. gfs2_assert_warn(ip->i_sbd, test_bit(GIF_MIN_INIT, &ip->i_flags));
  122. spin_lock(&ip->i_spin);
  123. if (ip->i_vnode)
  124. inode = igrab(ip->i_vnode);
  125. spin_unlock(&ip->i_spin);
  126. return inode;
  127. }
  128. /**
  129. * gfs2_ip2v - Get/Create a struct inode for a struct gfs2_inode
  130. * @ip: the struct gfs2_inode to get the struct inode for
  131. *
  132. * Returns: A VFS inode, or NULL if no mem
  133. */
  134. struct inode *gfs2_ip2v(struct gfs2_inode *ip)
  135. {
  136. struct inode *inode, *tmp;
  137. inode = gfs2_ip2v_lookup(ip);
  138. if (inode)
  139. return inode;
  140. tmp = new_inode(ip->i_sbd->sd_vfs);
  141. if (!tmp)
  142. return NULL;
  143. inode_attr_in(ip, tmp);
  144. if (S_ISREG(ip->i_di.di_mode)) {
  145. tmp->i_op = &gfs2_file_iops;
  146. tmp->i_fop = &gfs2_file_fops;
  147. tmp->i_mapping->a_ops = &gfs2_file_aops;
  148. } else if (S_ISDIR(ip->i_di.di_mode)) {
  149. tmp->i_op = &gfs2_dir_iops;
  150. tmp->i_fop = &gfs2_dir_fops;
  151. } else if (S_ISLNK(ip->i_di.di_mode)) {
  152. tmp->i_op = &gfs2_symlink_iops;
  153. } else {
  154. tmp->i_op = &gfs2_dev_iops;
  155. init_special_inode(tmp, tmp->i_mode, tmp->i_rdev);
  156. }
  157. tmp->u.generic_ip = NULL;
  158. for (;;) {
  159. spin_lock(&ip->i_spin);
  160. if (!ip->i_vnode)
  161. break;
  162. inode = igrab(ip->i_vnode);
  163. spin_unlock(&ip->i_spin);
  164. if (inode) {
  165. iput(tmp);
  166. return inode;
  167. }
  168. yield();
  169. }
  170. inode = tmp;
  171. gfs2_inode_hold(ip);
  172. ip->i_vnode = inode;
  173. inode->u.generic_ip = ip;
  174. spin_unlock(&ip->i_spin);
  175. insert_inode_hash(inode);
  176. return inode;
  177. }
  178. static int iget_test(struct inode *inode, void *opaque)
  179. {
  180. struct gfs2_inode *ip = inode->u.generic_ip;
  181. struct gfs2_inum *inum = (struct gfs2_inum *)opaque;
  182. if (ip && ip->i_num.no_addr == inum->no_addr)
  183. return 1;
  184. return 0;
  185. }
  186. struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum)
  187. {
  188. return ilookup5(sb, (unsigned long)inum->no_formal_ino,
  189. iget_test, inum);
  190. }
  191. void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type)
  192. {
  193. if (!test_and_set_bit(GIF_MIN_INIT, &ip->i_flags)) {
  194. ip->i_di.di_nlink = 1;
  195. ip->i_di.di_mode = DT2IF(type);
  196. }
  197. }
  198. /**
  199. * gfs2_inode_refresh - Refresh the incore copy of the dinode
  200. * @ip: The GFS2 inode
  201. *
  202. * Returns: errno
  203. */
  204. int gfs2_inode_refresh(struct gfs2_inode *ip)
  205. {
  206. struct buffer_head *dibh;
  207. int error;
  208. error = gfs2_meta_inode_buffer(ip, &dibh);
  209. if (error)
  210. return error;
  211. if (gfs2_metatype_check(ip->i_sbd, dibh, GFS2_METATYPE_DI)) {
  212. brelse(dibh);
  213. return -EIO;
  214. }
  215. gfs2_dinode_in(&ip->i_di, dibh->b_data);
  216. set_bit(GIF_MIN_INIT, &ip->i_flags);
  217. brelse(dibh);
  218. if (ip->i_num.no_addr != ip->i_di.di_num.no_addr) {
  219. if (gfs2_consist_inode(ip))
  220. gfs2_dinode_print(&ip->i_di);
  221. return -EIO;
  222. }
  223. if (ip->i_num.no_formal_ino != ip->i_di.di_num.no_formal_ino)
  224. return -ESTALE;
  225. ip->i_vn = ip->i_gl->gl_vn;
  226. return 0;
  227. }
  228. /**
  229. * inode_create - create a struct gfs2_inode
  230. * @i_gl: The glock covering the inode
  231. * @inum: The inode number
  232. * @io_gl: the iopen glock to acquire/hold (using holder in new gfs2_inode)
  233. * @io_state: the state the iopen glock should be acquired in
  234. * @ipp: pointer to put the returned inode in
  235. *
  236. * Returns: errno
  237. */
  238. static int inode_create(struct gfs2_glock *i_gl, const struct gfs2_inum *inum,
  239. struct gfs2_glock *io_gl, unsigned int io_state,
  240. struct gfs2_inode **ipp, int need_lock)
  241. {
  242. struct gfs2_sbd *sdp = i_gl->gl_sbd;
  243. struct gfs2_inode *ip;
  244. int error = 0;
  245. ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
  246. if (!ip)
  247. return -ENOMEM;
  248. memset(ip, 0, sizeof(struct gfs2_inode));
  249. ip->i_num = *inum;
  250. atomic_set(&ip->i_count, 1);
  251. ip->i_vn = i_gl->gl_vn - 1;
  252. ip->i_gl = i_gl;
  253. ip->i_sbd = sdp;
  254. spin_lock_init(&ip->i_spin);
  255. init_rwsem(&ip->i_rw_mutex);
  256. ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
  257. if (need_lock) {
  258. error = gfs2_glock_nq_init(io_gl,
  259. io_state, GL_LOCAL_EXCL | GL_EXACT,
  260. &ip->i_iopen_gh);
  261. if (error)
  262. goto fail;
  263. spin_lock(&io_gl->gl_spin);
  264. gfs2_glock_hold(i_gl);
  265. io_gl->gl_object = i_gl;
  266. spin_unlock(&io_gl->gl_spin);
  267. }
  268. gfs2_glock_hold(i_gl);
  269. i_gl->gl_object = ip;
  270. atomic_inc(&sdp->sd_inode_count);
  271. *ipp = ip;
  272. return 0;
  273. fail:
  274. gfs2_meta_cache_flush(ip);
  275. kmem_cache_free(gfs2_inode_cachep, ip);
  276. *ipp = NULL;
  277. return error;
  278. }
  279. /**
  280. * gfs2_inode_get - Create or get a reference on an inode
  281. * @i_gl: The glock covering the inode
  282. * @inum: The inode number
  283. * @create:
  284. * @ipp: pointer to put the returned inode in
  285. *
  286. * Returns: errno
  287. */
  288. int gfs2_inode_get(struct gfs2_glock *i_gl, const struct gfs2_inum *inum,
  289. int create, struct gfs2_inode **ipp)
  290. {
  291. struct gfs2_sbd *sdp = i_gl->gl_sbd;
  292. struct gfs2_glock *io_gl;
  293. int error = 0;
  294. gfs2_glmutex_lock(i_gl);
  295. *ipp = i_gl->gl_object;
  296. if (*ipp) {
  297. error = -ESTALE;
  298. if ((*ipp)->i_num.no_formal_ino != inum->no_formal_ino)
  299. goto out;
  300. atomic_inc(&(*ipp)->i_count);
  301. error = 0;
  302. goto out;
  303. }
  304. if (!create)
  305. goto out;
  306. error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops,
  307. CREATE, &io_gl);
  308. if (!error) {
  309. error = inode_create(i_gl, inum, io_gl, LM_ST_SHARED, ipp, 1);
  310. gfs2_glock_put(io_gl);
  311. }
  312. out:
  313. gfs2_glmutex_unlock(i_gl);
  314. return error;
  315. }
  316. void gfs2_inode_hold(struct gfs2_inode *ip)
  317. {
  318. gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
  319. atomic_inc(&ip->i_count);
  320. }
  321. void gfs2_inode_put(struct gfs2_inode *ip)
  322. {
  323. gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
  324. atomic_dec(&ip->i_count);
  325. }
  326. void gfs2_inode_destroy(struct gfs2_inode *ip, int unlock)
  327. {
  328. struct gfs2_sbd *sdp = ip->i_sbd;
  329. struct gfs2_glock *i_gl = ip->i_gl;
  330. gfs2_assert_warn(sdp, !atomic_read(&ip->i_count));
  331. if (unlock) {
  332. struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
  333. gfs2_assert(sdp, io_gl->gl_object == i_gl);
  334. spin_lock(&io_gl->gl_spin);
  335. io_gl->gl_object = NULL;
  336. spin_unlock(&io_gl->gl_spin);
  337. gfs2_glock_put(i_gl);
  338. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  339. }
  340. gfs2_meta_cache_flush(ip);
  341. kmem_cache_free(gfs2_inode_cachep, ip);
  342. i_gl->gl_object = NULL;
  343. gfs2_glock_put(i_gl);
  344. atomic_dec(&sdp->sd_inode_count);
  345. }
  346. static int dinode_dealloc(struct gfs2_inode *ip, struct gfs2_unlinked *ul)
  347. {
  348. struct gfs2_sbd *sdp = ip->i_sbd;
  349. struct gfs2_alloc *al;
  350. struct gfs2_rgrpd *rgd;
  351. int error;
  352. if (ip->i_di.di_blocks != 1) {
  353. if (gfs2_consist_inode(ip))
  354. gfs2_dinode_print(&ip->i_di);
  355. return -EIO;
  356. }
  357. al = gfs2_alloc_get(ip);
  358. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  359. if (error)
  360. goto out;
  361. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  362. if (error)
  363. goto out_qs;
  364. rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
  365. if (!rgd) {
  366. gfs2_consist_inode(ip);
  367. error = -EIO;
  368. goto out_rindex_relse;
  369. }
  370. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
  371. &al->al_rgd_gh);
  372. if (error)
  373. goto out_rindex_relse;
  374. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
  375. RES_STATFS + RES_QUOTA, 1);
  376. if (error)
  377. goto out_rg_gunlock;
  378. gfs2_trans_add_gl(ip->i_gl);
  379. gfs2_free_di(rgd, ip);
  380. error = gfs2_unlinked_ondisk_rm(sdp, ul);
  381. gfs2_trans_end(sdp);
  382. clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  383. out_rg_gunlock:
  384. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  385. out_rindex_relse:
  386. gfs2_glock_dq_uninit(&al->al_ri_gh);
  387. out_qs:
  388. gfs2_quota_unhold(ip);
  389. out:
  390. gfs2_alloc_put(ip);
  391. return error;
  392. }
  393. /**
  394. * inode_dealloc - Deallocate all on-disk blocks for an inode (dinode)
  395. * @sdp: the filesystem
  396. * @inum: the inode number to deallocate
  397. * @io_gh: a holder for the iopen glock for this inode
  398. *
  399. * N.B. When we enter this we already hold the iopen glock and getting
  400. * the glock for the inode means that we are grabbing the locks in the
  401. * "wrong" order so we must only so a try lock operation and fail if we
  402. * don't get the lock. Thats ok, since if we fail it means someone else
  403. * is using the inode still and thus we shouldn't be deallocating it
  404. * anyway.
  405. *
  406. * Returns: errno
  407. */
  408. static int inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul,
  409. struct gfs2_holder *io_gh)
  410. {
  411. struct gfs2_inode *ip;
  412. struct gfs2_holder i_gh;
  413. int error;
  414. error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
  415. &gfs2_inode_glops, LM_ST_EXCLUSIVE,
  416. LM_FLAG_TRY_1CB, &i_gh);
  417. switch(error) {
  418. case 0:
  419. break;
  420. case GLR_TRYFAILED:
  421. return 1; /* or back off and relock in different order? */
  422. default:
  423. return error;
  424. }
  425. gfs2_assert_warn(sdp, !i_gh.gh_gl->gl_object);
  426. error = inode_create(i_gh.gh_gl, &ul->ul_ut.ut_inum, io_gh->gh_gl,
  427. LM_ST_EXCLUSIVE, &ip, 0);
  428. if (error)
  429. goto out;
  430. error = gfs2_inode_refresh(ip);
  431. if (error)
  432. goto out_iput;
  433. if (ip->i_di.di_nlink) {
  434. if (gfs2_consist_inode(ip))
  435. gfs2_dinode_print(&ip->i_di);
  436. error = -EIO;
  437. goto out_iput;
  438. }
  439. if (S_ISDIR(ip->i_di.di_mode) &&
  440. (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
  441. error = gfs2_dir_exhash_dealloc(ip);
  442. if (error)
  443. goto out_iput;
  444. }
  445. if (ip->i_di.di_eattr) {
  446. error = gfs2_ea_dealloc(ip);
  447. if (error)
  448. goto out_iput;
  449. }
  450. if (!gfs2_is_stuffed(ip)) {
  451. error = gfs2_file_dealloc(ip);
  452. if (error)
  453. goto out_iput;
  454. }
  455. error = dinode_dealloc(ip, ul);
  456. if (error)
  457. goto out_iput;
  458. out_iput:
  459. gfs2_glmutex_lock(i_gh.gh_gl);
  460. gfs2_inode_put(ip);
  461. gfs2_inode_destroy(ip, 0);
  462. gfs2_glmutex_unlock(i_gh.gh_gl);
  463. out:
  464. gfs2_glock_dq_uninit(&i_gh);
  465. return error;
  466. }
  467. /**
  468. * try_inode_dealloc - Try to deallocate an inode and all its blocks
  469. * @sdp: the filesystem
  470. *
  471. * Returns: 0 on success, -errno on error, 1 on busy (inode open)
  472. */
  473. static int try_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
  474. {
  475. int error = 0;
  476. struct gfs2_holder iogh;
  477. gfs2_try_toss_inode(sdp, &ul->ul_ut.ut_inum);
  478. error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
  479. &gfs2_iopen_glops, LM_ST_EXCLUSIVE,
  480. LM_FLAG_TRY_1CB, &iogh);
  481. switch (error) {
  482. case 0:
  483. break;
  484. case GLR_TRYFAILED:
  485. return 1;
  486. default:
  487. return error;
  488. }
  489. error = inode_dealloc(sdp, ul, &iogh);
  490. gfs2_glock_dq_uninit(&iogh);
  491. return error;
  492. }
  493. static int inode_dealloc_uninit(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
  494. {
  495. struct gfs2_rgrpd *rgd;
  496. struct gfs2_holder ri_gh, rgd_gh;
  497. int error;
  498. error = gfs2_rindex_hold(sdp, &ri_gh);
  499. if (error)
  500. return error;
  501. rgd = gfs2_blk2rgrpd(sdp, ul->ul_ut.ut_inum.no_addr);
  502. if (!rgd) {
  503. gfs2_consist(sdp);
  504. error = -EIO;
  505. goto out;
  506. }
  507. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rgd_gh);
  508. if (error)
  509. goto out;
  510. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED + RES_STATFS, 0);
  511. if (error)
  512. goto out_gunlock;
  513. gfs2_free_uninit_di(rgd, ul->ul_ut.ut_inum.no_addr);
  514. gfs2_unlinked_ondisk_rm(sdp, ul);
  515. gfs2_trans_end(sdp);
  516. out_gunlock:
  517. gfs2_glock_dq_uninit(&rgd_gh);
  518. out:
  519. gfs2_glock_dq_uninit(&ri_gh);
  520. return error;
  521. }
  522. int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
  523. {
  524. if (ul->ul_ut.ut_flags & GFS2_UTF_UNINIT)
  525. return inode_dealloc_uninit(sdp, ul);
  526. else
  527. return try_inode_dealloc(sdp, ul);
  528. }
  529. /**
  530. * gfs2_change_nlink - Change nlink count on inode
  531. * @ip: The GFS2 inode
  532. * @diff: The change in the nlink count required
  533. *
  534. * Returns: errno
  535. */
  536. int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
  537. {
  538. struct buffer_head *dibh;
  539. uint32_t nlink;
  540. int error;
  541. nlink = ip->i_di.di_nlink + diff;
  542. /* If we are reducing the nlink count, but the new value ends up being
  543. bigger than the old one, we must have underflowed. */
  544. if (diff < 0 && nlink > ip->i_di.di_nlink) {
  545. if (gfs2_consist_inode(ip))
  546. gfs2_dinode_print(&ip->i_di);
  547. return -EIO;
  548. }
  549. error = gfs2_meta_inode_buffer(ip, &dibh);
  550. if (error)
  551. return error;
  552. ip->i_di.di_nlink = nlink;
  553. ip->i_di.di_ctime = get_seconds();
  554. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  555. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  556. brelse(dibh);
  557. return 0;
  558. }
  559. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  560. {
  561. struct qstr qstr;
  562. gfs2_str2qstr(&qstr, name);
  563. return gfs2_lookupi(dip, &qstr, 1, NULL);
  564. }
  565. /**
  566. * gfs2_lookupi - Look up a filename in a directory and return its inode
  567. * @d_gh: An initialized holder for the directory glock
  568. * @name: The name of the inode to look for
  569. * @is_root: If 1, ignore the caller's permissions
  570. * @i_gh: An uninitialized holder for the new inode glock
  571. *
  572. * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
  573. * @is_root is true.
  574. *
  575. * Returns: errno
  576. */
  577. struct inode *gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
  578. struct nameidata *nd)
  579. {
  580. struct super_block *sb = dir->i_sb;
  581. struct gfs2_inode *ipp;
  582. struct gfs2_inode *dip = dir->u.generic_ip;
  583. struct gfs2_sbd *sdp = dip->i_sbd;
  584. struct gfs2_holder d_gh;
  585. struct gfs2_inum inum;
  586. unsigned int type;
  587. struct gfs2_glock *gl;
  588. int error = 0;
  589. struct inode *inode = NULL;
  590. if (!name->len || name->len > GFS2_FNAMESIZE)
  591. return ERR_PTR(-ENAMETOOLONG);
  592. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  593. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  594. dir == sb->s_root->d_inode)) {
  595. gfs2_inode_hold(dip);
  596. ipp = dip;
  597. goto done;
  598. }
  599. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  600. if (error)
  601. return ERR_PTR(error);
  602. if (!is_root) {
  603. error = gfs2_repermission(dip->i_vnode, MAY_EXEC, NULL);
  604. if (error)
  605. goto out;
  606. }
  607. error = gfs2_dir_search(dir, name, &inum, &type);
  608. if (error)
  609. goto out;
  610. error = gfs2_glock_get(sdp, inum.no_addr, &gfs2_inode_glops,
  611. CREATE, &gl);
  612. if (error)
  613. goto out;
  614. error = gfs2_inode_get(gl, &inum, CREATE, &ipp);
  615. if (!error)
  616. gfs2_inode_min_init(ipp, type);
  617. gfs2_glock_put(gl);
  618. out:
  619. gfs2_glock_dq_uninit(&d_gh);
  620. done:
  621. if (error == -ENOENT)
  622. return NULL;
  623. if (error == 0) {
  624. inode = gfs2_ip2v(ipp);
  625. gfs2_inode_put(ipp);
  626. if (!inode)
  627. return ERR_PTR(-ENOMEM);
  628. return inode;
  629. }
  630. return ERR_PTR(error);
  631. }
  632. static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino)
  633. {
  634. struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
  635. struct buffer_head *bh;
  636. struct gfs2_inum_range ir;
  637. int error;
  638. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  639. if (error)
  640. return error;
  641. mutex_lock(&sdp->sd_inum_mutex);
  642. error = gfs2_meta_inode_buffer(ip, &bh);
  643. if (error) {
  644. mutex_unlock(&sdp->sd_inum_mutex);
  645. gfs2_trans_end(sdp);
  646. return error;
  647. }
  648. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  649. if (ir.ir_length) {
  650. *formal_ino = ir.ir_start++;
  651. ir.ir_length--;
  652. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  653. gfs2_inum_range_out(&ir,
  654. bh->b_data + sizeof(struct gfs2_dinode));
  655. brelse(bh);
  656. mutex_unlock(&sdp->sd_inum_mutex);
  657. gfs2_trans_end(sdp);
  658. return 0;
  659. }
  660. brelse(bh);
  661. mutex_unlock(&sdp->sd_inum_mutex);
  662. gfs2_trans_end(sdp);
  663. return 1;
  664. }
  665. static int pick_formal_ino_2(struct gfs2_sbd *sdp, uint64_t *formal_ino)
  666. {
  667. struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
  668. struct gfs2_inode *m_ip = sdp->sd_inum_inode->u.generic_ip;
  669. struct gfs2_holder gh;
  670. struct buffer_head *bh;
  671. struct gfs2_inum_range ir;
  672. int error;
  673. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  674. if (error)
  675. return error;
  676. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  677. if (error)
  678. goto out;
  679. mutex_lock(&sdp->sd_inum_mutex);
  680. error = gfs2_meta_inode_buffer(ip, &bh);
  681. if (error)
  682. goto out_end_trans;
  683. gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  684. if (!ir.ir_length) {
  685. struct buffer_head *m_bh;
  686. uint64_t x, y;
  687. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  688. if (error)
  689. goto out_brelse;
  690. x = *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode));
  691. x = y = be64_to_cpu(x);
  692. ir.ir_start = x;
  693. ir.ir_length = GFS2_INUM_QUANTUM;
  694. x += GFS2_INUM_QUANTUM;
  695. if (x < y)
  696. gfs2_consist_inode(m_ip);
  697. x = cpu_to_be64(x);
  698. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  699. *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = x;
  700. brelse(m_bh);
  701. }
  702. *formal_ino = ir.ir_start++;
  703. ir.ir_length--;
  704. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  705. gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
  706. out_brelse:
  707. brelse(bh);
  708. out_end_trans:
  709. mutex_unlock(&sdp->sd_inum_mutex);
  710. gfs2_trans_end(sdp);
  711. out:
  712. gfs2_glock_dq_uninit(&gh);
  713. return error;
  714. }
  715. static int pick_formal_ino(struct gfs2_sbd *sdp, uint64_t *inum)
  716. {
  717. int error;
  718. error = pick_formal_ino_1(sdp, inum);
  719. if (error <= 0)
  720. return error;
  721. error = pick_formal_ino_2(sdp, inum);
  722. return error;
  723. }
  724. /**
  725. * create_ok - OK to create a new on-disk inode here?
  726. * @dip: Directory in which dinode is to be created
  727. * @name: Name of new dinode
  728. * @mode:
  729. *
  730. * Returns: errno
  731. */
  732. static int create_ok(struct gfs2_inode *dip, struct qstr *name,
  733. unsigned int mode)
  734. {
  735. int error;
  736. error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
  737. if (error)
  738. return error;
  739. /* Don't create entries in an unlinked directory */
  740. if (!dip->i_di.di_nlink)
  741. return -EPERM;
  742. error = gfs2_dir_search(dip->i_vnode, name, NULL, NULL);
  743. switch (error) {
  744. case -ENOENT:
  745. error = 0;
  746. break;
  747. case 0:
  748. return -EEXIST;
  749. default:
  750. return error;
  751. }
  752. if (dip->i_di.di_entries == (uint32_t)-1)
  753. return -EFBIG;
  754. if (S_ISDIR(mode) && dip->i_di.di_nlink == (uint32_t)-1)
  755. return -EMLINK;
  756. return 0;
  757. }
  758. static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
  759. unsigned int *uid, unsigned int *gid)
  760. {
  761. if (dip->i_sbd->sd_args.ar_suiddir &&
  762. (dip->i_di.di_mode & S_ISUID) &&
  763. dip->i_di.di_uid) {
  764. if (S_ISDIR(*mode))
  765. *mode |= S_ISUID;
  766. else if (dip->i_di.di_uid != current->fsuid)
  767. *mode &= ~07111;
  768. *uid = dip->i_di.di_uid;
  769. } else
  770. *uid = current->fsuid;
  771. if (dip->i_di.di_mode & S_ISGID) {
  772. if (S_ISDIR(*mode))
  773. *mode |= S_ISGID;
  774. *gid = dip->i_di.di_gid;
  775. } else
  776. *gid = current->fsgid;
  777. }
  778. static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_unlinked *ul)
  779. {
  780. struct gfs2_sbd *sdp = dip->i_sbd;
  781. int error;
  782. gfs2_alloc_get(dip);
  783. dip->i_alloc.al_requested = RES_DINODE;
  784. error = gfs2_inplace_reserve(dip);
  785. if (error)
  786. goto out;
  787. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
  788. RES_STATFS, 0);
  789. if (error)
  790. goto out_ipreserv;
  791. ul->ul_ut.ut_inum.no_addr = gfs2_alloc_di(dip);
  792. ul->ul_ut.ut_flags = GFS2_UTF_UNINIT;
  793. error = gfs2_unlinked_ondisk_add(sdp, ul);
  794. gfs2_trans_end(sdp);
  795. out_ipreserv:
  796. gfs2_inplace_release(dip);
  797. out:
  798. gfs2_alloc_put(dip);
  799. return error;
  800. }
  801. /**
  802. * init_dinode - Fill in a new dinode structure
  803. * @dip: the directory this inode is being created in
  804. * @gl: The glock covering the new inode
  805. * @inum: the inode number
  806. * @mode: the file permissions
  807. * @uid:
  808. * @gid:
  809. *
  810. */
  811. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  812. struct gfs2_inum *inum, unsigned int mode,
  813. unsigned int uid, unsigned int gid)
  814. {
  815. struct gfs2_sbd *sdp = dip->i_sbd;
  816. struct gfs2_dinode *di;
  817. struct buffer_head *dibh;
  818. dibh = gfs2_meta_new(gl, inum->no_addr);
  819. gfs2_trans_add_bh(gl, dibh, 1);
  820. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  821. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  822. di = (struct gfs2_dinode *)dibh->b_data;
  823. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  824. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  825. di->di_mode = cpu_to_be32(mode);
  826. di->di_uid = cpu_to_be32(uid);
  827. di->di_gid = cpu_to_be32(gid);
  828. di->di_nlink = cpu_to_be32(0);
  829. di->di_size = cpu_to_be64(0);
  830. di->di_blocks = cpu_to_be64(1);
  831. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
  832. di->di_major = di->di_minor = cpu_to_be32(0);
  833. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  834. di->__pad[0] = di->__pad[1] = 0;
  835. di->di_flags = cpu_to_be32(0);
  836. if (S_ISREG(mode)) {
  837. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
  838. gfs2_tune_get(sdp, gt_new_files_jdata))
  839. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  840. if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
  841. gfs2_tune_get(sdp, gt_new_files_directio))
  842. di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
  843. } else if (S_ISDIR(mode)) {
  844. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  845. GFS2_DIF_INHERIT_DIRECTIO);
  846. di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
  847. GFS2_DIF_INHERIT_JDATA);
  848. }
  849. di->__pad1 = 0;
  850. di->di_height = cpu_to_be32(0);
  851. di->__pad2 = 0;
  852. di->__pad3 = 0;
  853. di->di_depth = cpu_to_be16(0);
  854. di->di_entries = cpu_to_be32(0);
  855. memset(&di->__pad4, 0, sizeof(di->__pad4));
  856. di->di_eattr = cpu_to_be64(0);
  857. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  858. brelse(dibh);
  859. }
  860. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  861. unsigned int mode, struct gfs2_unlinked *ul)
  862. {
  863. struct gfs2_sbd *sdp = dip->i_sbd;
  864. unsigned int uid, gid;
  865. int error;
  866. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  867. gfs2_alloc_get(dip);
  868. error = gfs2_quota_lock(dip, uid, gid);
  869. if (error)
  870. goto out;
  871. error = gfs2_quota_check(dip, uid, gid);
  872. if (error)
  873. goto out_quota;
  874. error = gfs2_trans_begin(sdp, RES_DINODE + RES_UNLINKED +
  875. RES_QUOTA, 0);
  876. if (error)
  877. goto out_quota;
  878. ul->ul_ut.ut_flags = 0;
  879. error = gfs2_unlinked_ondisk_munge(sdp, ul);
  880. init_dinode(dip, gl, &ul->ul_ut.ut_inum,
  881. mode, uid, gid);
  882. gfs2_quota_change(dip, +1, uid, gid);
  883. gfs2_trans_end(sdp);
  884. out_quota:
  885. gfs2_quota_unlock(dip);
  886. out:
  887. gfs2_alloc_put(dip);
  888. return error;
  889. }
  890. static int link_dinode(struct gfs2_inode *dip, struct qstr *name,
  891. struct gfs2_inode *ip, struct gfs2_unlinked *ul)
  892. {
  893. struct gfs2_sbd *sdp = dip->i_sbd;
  894. struct gfs2_alloc *al;
  895. int alloc_required;
  896. struct buffer_head *dibh;
  897. int error;
  898. al = gfs2_alloc_get(dip);
  899. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  900. if (error)
  901. goto fail;
  902. error = alloc_required = gfs2_diradd_alloc_required(dip->i_vnode, name);
  903. if (alloc_required < 0)
  904. goto fail;
  905. if (alloc_required) {
  906. error = gfs2_quota_check(dip, dip->i_di.di_uid,
  907. dip->i_di.di_gid);
  908. if (error)
  909. goto fail_quota_locks;
  910. al->al_requested = sdp->sd_max_dirres;
  911. error = gfs2_inplace_reserve(dip);
  912. if (error)
  913. goto fail_quota_locks;
  914. error = gfs2_trans_begin(sdp,
  915. sdp->sd_max_dirres +
  916. al->al_rgd->rd_ri.ri_length +
  917. 2 * RES_DINODE + RES_UNLINKED +
  918. RES_STATFS + RES_QUOTA, 0);
  919. if (error)
  920. goto fail_ipreserv;
  921. } else {
  922. error = gfs2_trans_begin(sdp,
  923. RES_LEAF +
  924. 2 * RES_DINODE +
  925. RES_UNLINKED, 0);
  926. if (error)
  927. goto fail_quota_locks;
  928. }
  929. error = gfs2_dir_add(dip->i_vnode, name, &ip->i_num, IF2DT(ip->i_di.di_mode));
  930. if (error)
  931. goto fail_end_trans;
  932. error = gfs2_meta_inode_buffer(ip, &dibh);
  933. if (error)
  934. goto fail_end_trans;
  935. ip->i_di.di_nlink = 1;
  936. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  937. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  938. brelse(dibh);
  939. error = gfs2_unlinked_ondisk_rm(sdp, ul);
  940. if (error)
  941. goto fail_end_trans;
  942. return 0;
  943. fail_end_trans:
  944. gfs2_trans_end(sdp);
  945. fail_ipreserv:
  946. if (dip->i_alloc.al_rgd)
  947. gfs2_inplace_release(dip);
  948. fail_quota_locks:
  949. gfs2_quota_unlock(dip);
  950. fail:
  951. gfs2_alloc_put(dip);
  952. return error;
  953. }
  954. /**
  955. * gfs2_createi - Create a new inode
  956. * @ghs: An array of two holders
  957. * @name: The name of the new file
  958. * @mode: the permissions on the new inode
  959. *
  960. * @ghs[0] is an initialized holder for the directory
  961. * @ghs[1] is the holder for the inode lock
  962. *
  963. * If the return value is not NULL, the glocks on both the directory and the new
  964. * file are held. A transaction has been started and an inplace reservation
  965. * is held, as well.
  966. *
  967. * Returns: An inode
  968. */
  969. struct inode *gfs2_createi(struct gfs2_holder *ghs, struct qstr *name,
  970. unsigned int mode)
  971. {
  972. struct inode *inode;
  973. struct gfs2_inode *dip = ghs->gh_gl->gl_object;
  974. struct gfs2_sbd *sdp = dip->i_sbd;
  975. struct gfs2_unlinked *ul;
  976. struct gfs2_inode *ip;
  977. int error;
  978. if (!name->len || name->len > GFS2_FNAMESIZE)
  979. return ERR_PTR(-ENAMETOOLONG);
  980. error = gfs2_unlinked_get(sdp, &ul);
  981. if (error)
  982. return ERR_PTR(error);
  983. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  984. error = gfs2_glock_nq(ghs);
  985. if (error)
  986. goto fail;
  987. error = create_ok(dip, name, mode);
  988. if (error)
  989. goto fail_gunlock;
  990. error = pick_formal_ino(sdp, &ul->ul_ut.ut_inum.no_formal_ino);
  991. if (error)
  992. goto fail_gunlock;
  993. error = alloc_dinode(dip, ul);
  994. if (error)
  995. goto fail_gunlock;
  996. if (ul->ul_ut.ut_inum.no_addr < dip->i_num.no_addr) {
  997. gfs2_glock_dq(ghs);
  998. error = gfs2_glock_nq_num(sdp,
  999. ul->ul_ut.ut_inum.no_addr,
  1000. &gfs2_inode_glops,
  1001. LM_ST_EXCLUSIVE, GL_SKIP,
  1002. ghs + 1);
  1003. if (error) {
  1004. gfs2_unlinked_put(sdp, ul);
  1005. return ERR_PTR(error);
  1006. }
  1007. gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
  1008. error = gfs2_glock_nq(ghs);
  1009. if (error) {
  1010. gfs2_glock_dq_uninit(ghs + 1);
  1011. gfs2_unlinked_put(sdp, ul);
  1012. return ERR_PTR(error);
  1013. }
  1014. error = create_ok(dip, name, mode);
  1015. if (error)
  1016. goto fail_gunlock2;
  1017. } else {
  1018. error = gfs2_glock_nq_num(sdp,
  1019. ul->ul_ut.ut_inum.no_addr,
  1020. &gfs2_inode_glops,
  1021. LM_ST_EXCLUSIVE, GL_SKIP,
  1022. ghs + 1);
  1023. if (error)
  1024. goto fail_gunlock;
  1025. }
  1026. error = make_dinode(dip, ghs[1].gh_gl, mode, ul);
  1027. if (error)
  1028. goto fail_gunlock2;
  1029. error = gfs2_inode_get(ghs[1].gh_gl, &ul->ul_ut.ut_inum, CREATE, &ip);
  1030. if (error)
  1031. goto fail_gunlock2;
  1032. error = gfs2_inode_refresh(ip);
  1033. if (error)
  1034. goto fail_iput;
  1035. error = gfs2_acl_create(dip, ip);
  1036. if (error)
  1037. goto fail_iput;
  1038. error = link_dinode(dip, name, ip, ul);
  1039. if (error)
  1040. goto fail_iput;
  1041. gfs2_unlinked_put(sdp, ul);
  1042. inode = gfs2_ip2v(ip);
  1043. gfs2_inode_put(ip);
  1044. if (!inode)
  1045. return ERR_PTR(-ENOMEM);
  1046. return inode;
  1047. fail_iput:
  1048. gfs2_inode_put(ip);
  1049. fail_gunlock2:
  1050. gfs2_glock_dq_uninit(ghs + 1);
  1051. fail_gunlock:
  1052. gfs2_glock_dq(ghs);
  1053. fail:
  1054. gfs2_unlinked_put(sdp, ul);
  1055. return ERR_PTR(error);
  1056. }
  1057. /**
  1058. * gfs2_unlinki - Unlink a file
  1059. * @dip: The inode of the directory
  1060. * @name: The name of the file to be unlinked
  1061. * @ip: The inode of the file to be removed
  1062. *
  1063. * Assumes Glocks on both dip and ip are held.
  1064. *
  1065. * Returns: errno
  1066. */
  1067. int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name,
  1068. struct gfs2_inode *ip, struct gfs2_unlinked *ul)
  1069. {
  1070. struct gfs2_sbd *sdp = dip->i_sbd;
  1071. int error;
  1072. error = gfs2_dir_del(dip, name);
  1073. if (error)
  1074. return error;
  1075. error = gfs2_change_nlink(ip, -1);
  1076. if (error)
  1077. return error;
  1078. /* If this inode is being unlinked from the directory structure,
  1079. we need to mark that in the log so that it isn't lost during
  1080. a crash. */
  1081. if (!ip->i_di.di_nlink) {
  1082. ul->ul_ut.ut_inum = ip->i_num;
  1083. error = gfs2_unlinked_ondisk_add(sdp, ul);
  1084. if (!error)
  1085. set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  1086. }
  1087. return error;
  1088. }
  1089. /**
  1090. * gfs2_rmdiri - Remove a directory
  1091. * @dip: The parent directory of the directory to be removed
  1092. * @name: The name of the directory to be removed
  1093. * @ip: The GFS2 inode of the directory to be removed
  1094. *
  1095. * Assumes Glocks on dip and ip are held
  1096. *
  1097. * Returns: errno
  1098. */
  1099. int gfs2_rmdiri(struct gfs2_inode *dip, struct qstr *name,
  1100. struct gfs2_inode *ip, struct gfs2_unlinked *ul)
  1101. {
  1102. struct gfs2_sbd *sdp = dip->i_sbd;
  1103. struct qstr dotname;
  1104. int error;
  1105. if (ip->i_di.di_entries != 2) {
  1106. if (gfs2_consist_inode(ip))
  1107. gfs2_dinode_print(&ip->i_di);
  1108. return -EIO;
  1109. }
  1110. error = gfs2_dir_del(dip, name);
  1111. if (error)
  1112. return error;
  1113. error = gfs2_change_nlink(dip, -1);
  1114. if (error)
  1115. return error;
  1116. gfs2_str2qstr(&dotname, ".");
  1117. error = gfs2_dir_del(ip, &dotname);
  1118. if (error)
  1119. return error;
  1120. dotname.len = 2;
  1121. dotname.name = "..";
  1122. dotname.hash = gfs2_disk_hash(dotname.name, dotname.len);
  1123. error = gfs2_dir_del(ip, &dotname);
  1124. if (error)
  1125. return error;
  1126. error = gfs2_change_nlink(ip, -2);
  1127. if (error)
  1128. return error;
  1129. /* This inode is being unlinked from the directory structure and
  1130. we need to mark that in the log so that it isn't lost during
  1131. a crash. */
  1132. ul->ul_ut.ut_inum = ip->i_num;
  1133. error = gfs2_unlinked_ondisk_add(sdp, ul);
  1134. if (!error)
  1135. set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
  1136. return error;
  1137. }
  1138. /*
  1139. * gfs2_unlink_ok - check to see that a inode is still in a directory
  1140. * @dip: the directory
  1141. * @name: the name of the file
  1142. * @ip: the inode
  1143. *
  1144. * Assumes that the lock on (at least) @dip is held.
  1145. *
  1146. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  1147. */
  1148. int gfs2_unlink_ok(struct gfs2_inode *dip, struct qstr *name,
  1149. struct gfs2_inode *ip)
  1150. {
  1151. struct gfs2_inum inum;
  1152. unsigned int type;
  1153. int error;
  1154. if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
  1155. return -EPERM;
  1156. if ((dip->i_di.di_mode & S_ISVTX) &&
  1157. dip->i_di.di_uid != current->fsuid &&
  1158. ip->i_di.di_uid != current->fsuid &&
  1159. !capable(CAP_FOWNER))
  1160. return -EPERM;
  1161. if (IS_APPEND(dip->i_vnode))
  1162. return -EPERM;
  1163. error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
  1164. if (error)
  1165. return error;
  1166. error = gfs2_dir_search(dip->i_vnode, name, &inum, &type);
  1167. if (error)
  1168. return error;
  1169. if (!gfs2_inum_equal(&inum, &ip->i_num))
  1170. return -ENOENT;
  1171. if (IF2DT(ip->i_di.di_mode) != type) {
  1172. gfs2_consist_inode(dip);
  1173. return -EIO;
  1174. }
  1175. return 0;
  1176. }
  1177. /*
  1178. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  1179. * @this: move this
  1180. * @to: to here
  1181. *
  1182. * Follow @to back to the root and make sure we don't encounter @this
  1183. * Assumes we already hold the rename lock.
  1184. *
  1185. * Returns: errno
  1186. */
  1187. int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1188. {
  1189. struct inode *dir = to->i_vnode;
  1190. struct super_block *sb = dir->i_sb;
  1191. struct inode *tmp;
  1192. struct qstr dotdot;
  1193. int error = 0;
  1194. gfs2_str2qstr(&dotdot, "..");
  1195. igrab(dir);
  1196. for (;;) {
  1197. if (dir == this->i_vnode) {
  1198. error = -EINVAL;
  1199. break;
  1200. }
  1201. if (dir == sb->s_root->d_inode) {
  1202. error = 0;
  1203. break;
  1204. }
  1205. tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
  1206. if (IS_ERR(tmp)) {
  1207. error = PTR_ERR(tmp);
  1208. break;
  1209. }
  1210. iput(dir);
  1211. dir = tmp;
  1212. }
  1213. iput(dir);
  1214. return error;
  1215. }
  1216. /**
  1217. * gfs2_readlinki - return the contents of a symlink
  1218. * @ip: the symlink's inode
  1219. * @buf: a pointer to the buffer to be filled
  1220. * @len: a pointer to the length of @buf
  1221. *
  1222. * If @buf is too small, a piece of memory is kmalloc()ed and needs
  1223. * to be freed by the caller.
  1224. *
  1225. * Returns: errno
  1226. */
  1227. int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
  1228. {
  1229. struct gfs2_holder i_gh;
  1230. struct buffer_head *dibh;
  1231. unsigned int x;
  1232. int error;
  1233. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
  1234. error = gfs2_glock_nq_atime(&i_gh);
  1235. if (error) {
  1236. gfs2_holder_uninit(&i_gh);
  1237. return error;
  1238. }
  1239. if (!ip->i_di.di_size) {
  1240. gfs2_consist_inode(ip);
  1241. error = -EIO;
  1242. goto out;
  1243. }
  1244. error = gfs2_meta_inode_buffer(ip, &dibh);
  1245. if (error)
  1246. goto out;
  1247. x = ip->i_di.di_size + 1;
  1248. if (x > *len) {
  1249. *buf = kmalloc(x, GFP_KERNEL);
  1250. if (!*buf) {
  1251. error = -ENOMEM;
  1252. goto out_brelse;
  1253. }
  1254. }
  1255. memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  1256. *len = x;
  1257. out_brelse:
  1258. brelse(dibh);
  1259. out:
  1260. gfs2_glock_dq_uninit(&i_gh);
  1261. return error;
  1262. }
  1263. /**
  1264. * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
  1265. * conditionally update the inode's atime
  1266. * @gh: the holder to acquire
  1267. *
  1268. * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
  1269. * Update if the difference between the current time and the inode's current
  1270. * atime is greater than an interval specified at mount.
  1271. *
  1272. * Returns: errno
  1273. */
  1274. int gfs2_glock_nq_atime(struct gfs2_holder *gh)
  1275. {
  1276. struct gfs2_glock *gl = gh->gh_gl;
  1277. struct gfs2_sbd *sdp = gl->gl_sbd;
  1278. struct gfs2_inode *ip = gl->gl_object;
  1279. int64_t curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
  1280. unsigned int state;
  1281. int flags;
  1282. int error;
  1283. if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
  1284. gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
  1285. gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
  1286. return -EINVAL;
  1287. state = gh->gh_state;
  1288. flags = gh->gh_flags;
  1289. error = gfs2_glock_nq(gh);
  1290. if (error)
  1291. return error;
  1292. if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
  1293. (sdp->sd_vfs->s_flags & MS_RDONLY))
  1294. return 0;
  1295. curtime = get_seconds();
  1296. if (curtime - ip->i_di.di_atime >= quantum) {
  1297. gfs2_glock_dq(gh);
  1298. gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
  1299. gh);
  1300. error = gfs2_glock_nq(gh);
  1301. if (error)
  1302. return error;
  1303. /* Verify that atime hasn't been updated while we were
  1304. trying to get exclusive lock. */
  1305. curtime = get_seconds();
  1306. if (curtime - ip->i_di.di_atime >= quantum) {
  1307. struct buffer_head *dibh;
  1308. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1309. if (error == -EROFS)
  1310. return 0;
  1311. if (error)
  1312. goto fail;
  1313. error = gfs2_meta_inode_buffer(ip, &dibh);
  1314. if (error)
  1315. goto fail_end_trans;
  1316. ip->i_di.di_atime = curtime;
  1317. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1318. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1319. brelse(dibh);
  1320. gfs2_trans_end(sdp);
  1321. }
  1322. /* If someone else has asked for the glock,
  1323. unlock and let them have it. Then reacquire
  1324. in the original state. */
  1325. if (gfs2_glock_is_blocking(gl)) {
  1326. gfs2_glock_dq(gh);
  1327. gfs2_holder_reinit(state, flags, gh);
  1328. return gfs2_glock_nq(gh);
  1329. }
  1330. }
  1331. return 0;
  1332. fail_end_trans:
  1333. gfs2_trans_end(sdp);
  1334. fail:
  1335. gfs2_glock_dq(gh);
  1336. return error;
  1337. }
  1338. /**
  1339. * glock_compare_atime - Compare two struct gfs2_glock structures for sort
  1340. * @arg_a: the first structure
  1341. * @arg_b: the second structure
  1342. *
  1343. * Returns: 1 if A > B
  1344. * -1 if A < B
  1345. * 0 if A = B
  1346. */
  1347. static int glock_compare_atime(const void *arg_a, const void *arg_b)
  1348. {
  1349. struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
  1350. struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
  1351. struct lm_lockname *a = &gh_a->gh_gl->gl_name;
  1352. struct lm_lockname *b = &gh_b->gh_gl->gl_name;
  1353. int ret = 0;
  1354. if (a->ln_number > b->ln_number)
  1355. ret = 1;
  1356. else if (a->ln_number < b->ln_number)
  1357. ret = -1;
  1358. else {
  1359. if (gh_a->gh_state == LM_ST_SHARED &&
  1360. gh_b->gh_state == LM_ST_EXCLUSIVE)
  1361. ret = 1;
  1362. else if (gh_a->gh_state == LM_ST_SHARED &&
  1363. (gh_b->gh_flags & GL_ATIME))
  1364. ret = 1;
  1365. }
  1366. return ret;
  1367. }
  1368. /**
  1369. * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
  1370. * atime update
  1371. * @num_gh: the number of structures
  1372. * @ghs: an array of struct gfs2_holder structures
  1373. *
  1374. * Returns: 0 on success (all glocks acquired),
  1375. * errno on failure (no glocks acquired)
  1376. */
  1377. int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
  1378. {
  1379. struct gfs2_holder **p;
  1380. unsigned int x;
  1381. int error = 0;
  1382. if (!num_gh)
  1383. return 0;
  1384. if (num_gh == 1) {
  1385. ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1386. if (ghs->gh_flags & GL_ATIME)
  1387. error = gfs2_glock_nq_atime(ghs);
  1388. else
  1389. error = gfs2_glock_nq(ghs);
  1390. return error;
  1391. }
  1392. p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
  1393. if (!p)
  1394. return -ENOMEM;
  1395. for (x = 0; x < num_gh; x++)
  1396. p[x] = &ghs[x];
  1397. sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
  1398. for (x = 0; x < num_gh; x++) {
  1399. p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
  1400. if (p[x]->gh_flags & GL_ATIME)
  1401. error = gfs2_glock_nq_atime(p[x]);
  1402. else
  1403. error = gfs2_glock_nq(p[x]);
  1404. if (error) {
  1405. while (x--)
  1406. gfs2_glock_dq(p[x]);
  1407. break;
  1408. }
  1409. }
  1410. kfree(p);
  1411. return error;
  1412. }
  1413. /**
  1414. * gfs2_try_toss_vnode - See if we can toss a vnode from memory
  1415. * @ip: the inode
  1416. *
  1417. * Returns: 1 if the vnode was tossed
  1418. */
  1419. void gfs2_try_toss_vnode(struct gfs2_inode *ip)
  1420. {
  1421. struct inode *inode;
  1422. inode = gfs2_ip2v_lookup(ip);
  1423. if (!inode)
  1424. return;
  1425. d_prune_aliases(inode);
  1426. if (S_ISDIR(ip->i_di.di_mode)) {
  1427. struct list_head *head = &inode->i_dentry;
  1428. struct dentry *d = NULL;
  1429. spin_lock(&dcache_lock);
  1430. if (list_empty(head))
  1431. spin_unlock(&dcache_lock);
  1432. else {
  1433. d = list_entry(head->next, struct dentry, d_alias);
  1434. dget_locked(d);
  1435. spin_unlock(&dcache_lock);
  1436. if (have_submounts(d))
  1437. dput(d);
  1438. else {
  1439. shrink_dcache_parent(d);
  1440. dput(d);
  1441. d_prune_aliases(inode);
  1442. }
  1443. }
  1444. }
  1445. inode->i_nlink = 0;
  1446. iput(inode);
  1447. }
  1448. static int
  1449. __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1450. {
  1451. struct buffer_head *dibh;
  1452. int error;
  1453. error = gfs2_meta_inode_buffer(ip, &dibh);
  1454. if (!error) {
  1455. error = inode_setattr(ip->i_vnode, attr);
  1456. gfs2_assert_warn(ip->i_sbd, !error);
  1457. gfs2_inode_attr_out(ip);
  1458. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1459. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  1460. brelse(dibh);
  1461. }
  1462. return error;
  1463. }
  1464. /**
  1465. * gfs2_setattr_simple -
  1466. * @ip:
  1467. * @attr:
  1468. *
  1469. * Called with a reference on the vnode.
  1470. *
  1471. * Returns: errno
  1472. */
  1473. int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
  1474. {
  1475. int error;
  1476. if (current->journal_info)
  1477. return __gfs2_setattr_simple(ip, attr);
  1478. error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
  1479. if (error)
  1480. return error;
  1481. error = __gfs2_setattr_simple(ip, attr);
  1482. gfs2_trans_end(ip->i_sbd);
  1483. return error;
  1484. }
  1485. int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd)
  1486. {
  1487. return permission(inode, mask, nd);
  1488. }