inode.c 39 KB

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