inode.c 39 KB

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