inode.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2011 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 version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/security.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. #include "glops.h"
  37. struct gfs2_skip_data {
  38. u64 no_addr;
  39. int skipped;
  40. int non_block;
  41. };
  42. static int iget_test(struct inode *inode, void *opaque)
  43. {
  44. struct gfs2_inode *ip = GFS2_I(inode);
  45. struct gfs2_skip_data *data = opaque;
  46. if (ip->i_no_addr == data->no_addr) {
  47. if (data->non_block &&
  48. inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  49. data->skipped = 1;
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int iget_set(struct inode *inode, void *opaque)
  57. {
  58. struct gfs2_inode *ip = GFS2_I(inode);
  59. struct gfs2_skip_data *data = opaque;
  60. if (data->skipped)
  61. return -ENOENT;
  62. inode->i_ino = (unsigned long)(data->no_addr);
  63. ip->i_no_addr = data->no_addr;
  64. return 0;
  65. }
  66. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
  67. {
  68. unsigned long hash = (unsigned long)no_addr;
  69. struct gfs2_skip_data data;
  70. data.no_addr = no_addr;
  71. data.skipped = 0;
  72. data.non_block = non_block;
  73. return ilookup5(sb, hash, iget_test, &data);
  74. }
  75. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
  76. int non_block)
  77. {
  78. struct gfs2_skip_data data;
  79. unsigned long hash = (unsigned long)no_addr;
  80. data.no_addr = no_addr;
  81. data.skipped = 0;
  82. data.non_block = non_block;
  83. return iget5_locked(sb, hash, iget_test, iget_set, &data);
  84. }
  85. /**
  86. * gfs2_set_iop - Sets inode operations
  87. * @inode: The inode with correct i_mode filled in
  88. *
  89. * GFS2 lookup code fills in vfs inode contents based on info obtained
  90. * from directory entry inside gfs2_inode_lookup().
  91. */
  92. static void gfs2_set_iop(struct inode *inode)
  93. {
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. umode_t mode = inode->i_mode;
  96. if (S_ISREG(mode)) {
  97. inode->i_op = &gfs2_file_iops;
  98. if (gfs2_localflocks(sdp))
  99. inode->i_fop = &gfs2_file_fops_nolock;
  100. else
  101. inode->i_fop = &gfs2_file_fops;
  102. } else if (S_ISDIR(mode)) {
  103. inode->i_op = &gfs2_dir_iops;
  104. if (gfs2_localflocks(sdp))
  105. inode->i_fop = &gfs2_dir_fops_nolock;
  106. else
  107. inode->i_fop = &gfs2_dir_fops;
  108. } else if (S_ISLNK(mode)) {
  109. inode->i_op = &gfs2_symlink_iops;
  110. } else {
  111. inode->i_op = &gfs2_file_iops;
  112. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  113. }
  114. }
  115. /**
  116. * gfs2_inode_lookup - Lookup an inode
  117. * @sb: The super block
  118. * @no_addr: The inode number
  119. * @type: The type of the inode
  120. * non_block: Can we block on inodes that are being freed?
  121. *
  122. * Returns: A VFS inode, or an error
  123. */
  124. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  125. u64 no_addr, u64 no_formal_ino, int non_block)
  126. {
  127. struct inode *inode;
  128. struct gfs2_inode *ip;
  129. struct gfs2_glock *io_gl = NULL;
  130. int error;
  131. inode = gfs2_iget(sb, no_addr, non_block);
  132. ip = GFS2_I(inode);
  133. if (!inode)
  134. return ERR_PTR(-ENOBUFS);
  135. if (inode->i_state & I_NEW) {
  136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  137. ip->i_no_formal_ino = no_formal_ino;
  138. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  139. if (unlikely(error))
  140. goto fail;
  141. ip->i_gl->gl_object = ip;
  142. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  143. if (unlikely(error))
  144. goto fail_put;
  145. set_bit(GIF_INVALID, &ip->i_flags);
  146. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  147. if (unlikely(error))
  148. goto fail_iopen;
  149. ip->i_iopen_gh.gh_gl->gl_object = ip;
  150. gfs2_glock_put(io_gl);
  151. io_gl = NULL;
  152. if (type == DT_UNKNOWN) {
  153. /* Inode glock must be locked already */
  154. error = gfs2_inode_refresh(GFS2_I(inode));
  155. if (error)
  156. goto fail_refresh;
  157. } else {
  158. inode->i_mode = DT2IF(type);
  159. }
  160. gfs2_set_iop(inode);
  161. unlock_new_inode(inode);
  162. }
  163. return inode;
  164. fail_refresh:
  165. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  166. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  167. fail_iopen:
  168. if (io_gl)
  169. gfs2_glock_put(io_gl);
  170. fail_put:
  171. ip->i_gl->gl_object = NULL;
  172. gfs2_glock_put(ip->i_gl);
  173. fail:
  174. iget_failed(inode);
  175. return ERR_PTR(error);
  176. }
  177. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  178. u64 *no_formal_ino, unsigned int blktype)
  179. {
  180. struct super_block *sb = sdp->sd_vfs;
  181. struct gfs2_holder i_gh;
  182. struct inode *inode = NULL;
  183. int error;
  184. /* Must not read in block until block type is verified */
  185. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  186. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  187. if (error)
  188. return ERR_PTR(error);
  189. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  190. if (error)
  191. goto fail;
  192. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  193. if (IS_ERR(inode))
  194. goto fail;
  195. /* Two extra checks for NFS only */
  196. if (no_formal_ino) {
  197. error = -ESTALE;
  198. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  199. goto fail_iput;
  200. error = -EIO;
  201. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  202. goto fail_iput;
  203. error = 0;
  204. }
  205. fail:
  206. gfs2_glock_dq_uninit(&i_gh);
  207. return error ? ERR_PTR(error) : inode;
  208. fail_iput:
  209. iput(inode);
  210. goto fail;
  211. }
  212. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  213. {
  214. struct qstr qstr;
  215. struct inode *inode;
  216. gfs2_str2qstr(&qstr, name);
  217. inode = gfs2_lookupi(dip, &qstr, 1);
  218. /* gfs2_lookupi has inconsistent callers: vfs
  219. * related routines expect NULL for no entry found,
  220. * gfs2_lookup_simple callers expect ENOENT
  221. * and do not check for NULL.
  222. */
  223. if (inode == NULL)
  224. return ERR_PTR(-ENOENT);
  225. else
  226. return inode;
  227. }
  228. /**
  229. * gfs2_lookupi - Look up a filename in a directory and return its inode
  230. * @d_gh: An initialized holder for the directory glock
  231. * @name: The name of the inode to look for
  232. * @is_root: If 1, ignore the caller's permissions
  233. * @i_gh: An uninitialized holder for the new inode glock
  234. *
  235. * This can be called via the VFS filldir function when NFS is doing
  236. * a readdirplus and the inode which its intending to stat isn't
  237. * already in cache. In this case we must not take the directory glock
  238. * again, since the readdir call will have already taken that lock.
  239. *
  240. * Returns: errno
  241. */
  242. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  243. int is_root)
  244. {
  245. struct super_block *sb = dir->i_sb;
  246. struct gfs2_inode *dip = GFS2_I(dir);
  247. struct gfs2_holder d_gh;
  248. int error = 0;
  249. struct inode *inode = NULL;
  250. int unlock = 0;
  251. if (!name->len || name->len > GFS2_FNAMESIZE)
  252. return ERR_PTR(-ENAMETOOLONG);
  253. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  254. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  255. dir == sb->s_root->d_inode)) {
  256. igrab(dir);
  257. return dir;
  258. }
  259. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  260. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  261. if (error)
  262. return ERR_PTR(error);
  263. unlock = 1;
  264. }
  265. if (!is_root) {
  266. error = gfs2_permission(dir, MAY_EXEC);
  267. if (error)
  268. goto out;
  269. }
  270. inode = gfs2_dir_search(dir, name);
  271. if (IS_ERR(inode))
  272. error = PTR_ERR(inode);
  273. out:
  274. if (unlock)
  275. gfs2_glock_dq_uninit(&d_gh);
  276. if (error == -ENOENT)
  277. return NULL;
  278. return inode ? inode : ERR_PTR(error);
  279. }
  280. /**
  281. * create_ok - OK to create a new on-disk inode here?
  282. * @dip: Directory in which dinode is to be created
  283. * @name: Name of new dinode
  284. * @mode:
  285. *
  286. * Returns: errno
  287. */
  288. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  289. umode_t mode)
  290. {
  291. int error;
  292. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  293. if (error)
  294. return error;
  295. /* Don't create entries in an unlinked directory */
  296. if (!dip->i_inode.i_nlink)
  297. return -ENOENT;
  298. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  299. switch (error) {
  300. case -ENOENT:
  301. error = 0;
  302. break;
  303. case 0:
  304. return -EEXIST;
  305. default:
  306. return error;
  307. }
  308. if (dip->i_entries == (u32)-1)
  309. return -EFBIG;
  310. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  311. return -EMLINK;
  312. return 0;
  313. }
  314. static void munge_mode_uid_gid(const struct gfs2_inode *dip,
  315. struct inode *inode)
  316. {
  317. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  318. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  319. if (S_ISDIR(inode->i_mode))
  320. inode->i_mode |= S_ISUID;
  321. else if (dip->i_inode.i_uid != current_fsuid())
  322. inode->i_mode &= ~07111;
  323. inode->i_uid = dip->i_inode.i_uid;
  324. } else
  325. inode->i_uid = current_fsuid();
  326. if (dip->i_inode.i_mode & S_ISGID) {
  327. if (S_ISDIR(inode->i_mode))
  328. inode->i_mode |= S_ISGID;
  329. inode->i_gid = dip->i_inode.i_gid;
  330. } else
  331. inode->i_gid = current_fsgid();
  332. }
  333. static int alloc_dinode(struct gfs2_inode *ip, u32 flags)
  334. {
  335. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  336. int error;
  337. int dblocks = 1;
  338. error = gfs2_inplace_reserve(ip, RES_DINODE, flags);
  339. if (error)
  340. goto out;
  341. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  342. if (error)
  343. goto out_ipreserv;
  344. error = gfs2_alloc_blocks(ip, &ip->i_no_addr, &dblocks, 1, &ip->i_generation);
  345. ip->i_no_formal_ino = ip->i_generation;
  346. ip->i_inode.i_ino = ip->i_no_addr;
  347. ip->i_goal = ip->i_no_addr;
  348. gfs2_trans_end(sdp);
  349. out_ipreserv:
  350. gfs2_inplace_release(ip);
  351. out:
  352. return error;
  353. }
  354. static void gfs2_init_dir(struct buffer_head *dibh,
  355. const struct gfs2_inode *parent)
  356. {
  357. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  358. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  359. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  360. dent->de_inum = di->di_num; /* already GFS2 endian */
  361. dent->de_type = cpu_to_be16(DT_DIR);
  362. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  363. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  364. gfs2_inum_out(parent, dent);
  365. dent->de_type = cpu_to_be16(DT_DIR);
  366. }
  367. /**
  368. * init_dinode - Fill in a new dinode structure
  369. * @dip: The directory this inode is being created in
  370. * @ip: The inode
  371. * @symname: The symlink destination (if a symlink)
  372. * @bhp: The buffer head (returned to caller)
  373. *
  374. */
  375. static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  376. const char *symname, struct buffer_head **bhp)
  377. {
  378. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  379. struct gfs2_dinode *di;
  380. struct buffer_head *dibh;
  381. struct timespec tv = CURRENT_TIME;
  382. dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
  383. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  384. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  385. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  386. di = (struct gfs2_dinode *)dibh->b_data;
  387. di->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  388. di->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  389. di->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  390. di->di_uid = cpu_to_be32(ip->i_inode.i_uid);
  391. di->di_gid = cpu_to_be32(ip->i_inode.i_gid);
  392. di->di_nlink = 0;
  393. di->di_size = cpu_to_be64(ip->i_inode.i_size);
  394. di->di_blocks = cpu_to_be64(1);
  395. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  396. di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
  397. di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
  398. di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_no_addr);
  399. di->di_generation = cpu_to_be64(ip->i_generation);
  400. di->di_flags = 0;
  401. di->__pad1 = 0;
  402. di->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) ? GFS2_FORMAT_DE : 0);
  403. di->di_height = 0;
  404. di->__pad2 = 0;
  405. di->__pad3 = 0;
  406. di->di_depth = 0;
  407. di->di_entries = 0;
  408. memset(&di->__pad4, 0, sizeof(di->__pad4));
  409. di->di_eattr = 0;
  410. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  411. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  412. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  413. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  414. switch(ip->i_inode.i_mode & S_IFMT) {
  415. case S_IFREG:
  416. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  417. gfs2_tune_get(sdp, gt_new_files_jdata))
  418. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  419. break;
  420. case S_IFDIR:
  421. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  422. GFS2_DIF_INHERIT_JDATA);
  423. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  424. di->di_size = cpu_to_be64(sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  425. di->di_entries = cpu_to_be32(2);
  426. gfs2_init_dir(dibh, dip);
  427. break;
  428. case S_IFLNK:
  429. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
  430. break;
  431. }
  432. set_buffer_uptodate(dibh);
  433. *bhp = dibh;
  434. }
  435. static int make_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
  436. const char *symname, struct buffer_head **bhp)
  437. {
  438. struct inode *inode = &ip->i_inode;
  439. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  440. int error;
  441. error = gfs2_rindex_update(sdp);
  442. if (error)
  443. return error;
  444. error = gfs2_quota_lock(dip, inode->i_uid, inode->i_gid);
  445. if (error)
  446. return error;
  447. error = gfs2_quota_check(dip, inode->i_uid, inode->i_gid);
  448. if (error)
  449. goto out_quota;
  450. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  451. if (error)
  452. goto out_quota;
  453. init_dinode(dip, ip, symname, bhp);
  454. gfs2_quota_change(dip, +1, inode->i_uid, inode->i_gid);
  455. gfs2_trans_end(sdp);
  456. out_quota:
  457. gfs2_quota_unlock(dip);
  458. return error;
  459. }
  460. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  461. struct gfs2_inode *ip)
  462. {
  463. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  464. int alloc_required;
  465. struct buffer_head *dibh;
  466. int error;
  467. error = gfs2_rindex_update(sdp);
  468. if (error)
  469. return error;
  470. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  471. if (error)
  472. goto fail;
  473. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  474. if (alloc_required < 0)
  475. goto fail_quota_locks;
  476. if (alloc_required) {
  477. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  478. if (error)
  479. goto fail_quota_locks;
  480. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres, 0);
  481. if (error)
  482. goto fail_quota_locks;
  483. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  484. dip->i_rgd->rd_length +
  485. 2 * RES_DINODE +
  486. RES_STATFS + RES_QUOTA, 0);
  487. if (error)
  488. goto fail_ipreserv;
  489. } else {
  490. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  491. if (error)
  492. goto fail_quota_locks;
  493. }
  494. error = gfs2_dir_add(&dip->i_inode, name, ip);
  495. if (error)
  496. goto fail_end_trans;
  497. error = gfs2_meta_inode_buffer(ip, &dibh);
  498. if (error)
  499. goto fail_end_trans;
  500. set_nlink(&ip->i_inode, S_ISDIR(ip->i_inode.i_mode) ? 2 : 1);
  501. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  502. gfs2_dinode_out(ip, dibh->b_data);
  503. brelse(dibh);
  504. return 0;
  505. fail_end_trans:
  506. gfs2_trans_end(sdp);
  507. fail_ipreserv:
  508. if (alloc_required)
  509. gfs2_inplace_release(dip);
  510. fail_quota_locks:
  511. gfs2_quota_unlock(dip);
  512. fail:
  513. return error;
  514. }
  515. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  516. void *fs_info)
  517. {
  518. const struct xattr *xattr;
  519. int err = 0;
  520. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  521. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  522. xattr->value_len, 0,
  523. GFS2_EATYPE_SECURITY);
  524. if (err < 0)
  525. break;
  526. }
  527. return err;
  528. }
  529. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  530. const struct qstr *qstr)
  531. {
  532. return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  533. &gfs2_initxattrs, NULL);
  534. }
  535. /**
  536. * gfs2_create_inode - Create a new inode
  537. * @dir: The parent directory
  538. * @dentry: The new dentry
  539. * @mode: The permissions on the new inode
  540. * @dev: For device nodes, this is the device number
  541. * @symname: For symlinks, this is the link destination
  542. * @size: The initial size of the inode (ignored for directories)
  543. *
  544. * Returns: 0 on success, or error code
  545. */
  546. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  547. umode_t mode, dev_t dev, const char *symname,
  548. unsigned int size, int excl)
  549. {
  550. const struct qstr *name = &dentry->d_name;
  551. struct gfs2_holder ghs[2];
  552. struct inode *inode = NULL;
  553. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  554. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  555. struct gfs2_glock *io_gl;
  556. int error;
  557. struct buffer_head *bh = NULL;
  558. u32 aflags = 0;
  559. if (!name->len || name->len > GFS2_FNAMESIZE)
  560. return -ENAMETOOLONG;
  561. error = gfs2_rs_alloc(dip);
  562. if (error)
  563. return error;
  564. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  565. if (error)
  566. goto fail;
  567. error = create_ok(dip, name, mode);
  568. if ((error == -EEXIST) && S_ISREG(mode) && !excl) {
  569. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  570. gfs2_glock_dq_uninit(ghs);
  571. d_instantiate(dentry, inode);
  572. return IS_ERR(inode) ? PTR_ERR(inode) : 0;
  573. }
  574. if (error)
  575. goto fail_gunlock;
  576. inode = new_inode(sdp->sd_vfs);
  577. if (!inode) {
  578. gfs2_glock_dq_uninit(ghs);
  579. return -ENOMEM;
  580. }
  581. ip = GFS2_I(inode);
  582. error = gfs2_rs_alloc(ip);
  583. if (error)
  584. goto fail_free_inode;
  585. set_bit(GIF_INVALID, &ip->i_flags);
  586. inode->i_mode = mode;
  587. inode->i_rdev = dev;
  588. inode->i_size = size;
  589. munge_mode_uid_gid(dip, inode);
  590. ip->i_goal = dip->i_goal;
  591. if ((GFS2_I(sdp->sd_root_dir->d_inode) == dip) ||
  592. (dip->i_diskflags & GFS2_DIF_TOPDIR))
  593. aflags |= GFS2_AF_ORLOV;
  594. error = alloc_dinode(ip, aflags);
  595. if (error)
  596. goto fail_free_inode;
  597. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  598. if (error)
  599. goto fail_free_inode;
  600. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  601. if (error)
  602. goto fail_free_inode;
  603. error = make_dinode(dip, ip, symname, &bh);
  604. if (error)
  605. goto fail_gunlock2;
  606. error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  607. if (error)
  608. goto fail_gunlock2;
  609. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  610. if (error)
  611. goto fail_gunlock2;
  612. ip->i_iopen_gh.gh_gl->gl_object = ip;
  613. gfs2_glock_put(io_gl);
  614. gfs2_set_iop(inode);
  615. insert_inode_hash(inode);
  616. error = gfs2_inode_refresh(ip);
  617. if (error)
  618. goto fail_gunlock3;
  619. error = gfs2_acl_create(dip, inode);
  620. if (error)
  621. goto fail_gunlock3;
  622. error = gfs2_security_init(dip, ip, name);
  623. if (error)
  624. goto fail_gunlock3;
  625. error = link_dinode(dip, name, ip);
  626. if (error)
  627. goto fail_gunlock3;
  628. if (bh)
  629. brelse(bh);
  630. gfs2_trans_end(sdp);
  631. gfs2_inplace_release(dip);
  632. gfs2_quota_unlock(dip);
  633. mark_inode_dirty(inode);
  634. gfs2_glock_dq_uninit_m(2, ghs);
  635. d_instantiate(dentry, inode);
  636. return 0;
  637. fail_gunlock3:
  638. gfs2_glock_dq_uninit(ghs + 1);
  639. if (ip->i_gl)
  640. gfs2_glock_put(ip->i_gl);
  641. goto fail_gunlock;
  642. fail_gunlock2:
  643. gfs2_glock_dq_uninit(ghs + 1);
  644. fail_free_inode:
  645. if (ip->i_gl)
  646. gfs2_glock_put(ip->i_gl);
  647. gfs2_rs_delete(ip);
  648. free_inode_nonrcu(inode);
  649. inode = NULL;
  650. fail_gunlock:
  651. gfs2_glock_dq_uninit(ghs);
  652. if (inode && !IS_ERR(inode)) {
  653. set_bit(GIF_ALLOC_FAILED, &GFS2_I(inode)->i_flags);
  654. iput(inode);
  655. }
  656. fail:
  657. if (bh)
  658. brelse(bh);
  659. return error;
  660. }
  661. /**
  662. * gfs2_create - Create a file
  663. * @dir: The directory in which to create the file
  664. * @dentry: The dentry of the new file
  665. * @mode: The mode of the new file
  666. *
  667. * Returns: errno
  668. */
  669. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  670. umode_t mode, bool excl)
  671. {
  672. return gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0, excl);
  673. }
  674. /**
  675. * gfs2_lookup - Look up a filename in a directory and return its inode
  676. * @dir: The directory inode
  677. * @dentry: The dentry of the new inode
  678. * @nd: passed from Linux VFS, ignored by us
  679. *
  680. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  681. *
  682. * Returns: errno
  683. */
  684. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  685. unsigned int flags)
  686. {
  687. struct inode *inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  688. if (inode && !IS_ERR(inode)) {
  689. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  690. struct gfs2_holder gh;
  691. int error;
  692. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  693. if (error) {
  694. iput(inode);
  695. return ERR_PTR(error);
  696. }
  697. gfs2_glock_dq_uninit(&gh);
  698. }
  699. return d_splice_alias(inode, dentry);
  700. }
  701. /**
  702. * gfs2_link - Link to a file
  703. * @old_dentry: The inode to link
  704. * @dir: Add link to this directory
  705. * @dentry: The name of the link
  706. *
  707. * Link the inode in "old_dentry" into the directory "dir" with the
  708. * name in "dentry".
  709. *
  710. * Returns: errno
  711. */
  712. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  713. struct dentry *dentry)
  714. {
  715. struct gfs2_inode *dip = GFS2_I(dir);
  716. struct gfs2_sbd *sdp = GFS2_SB(dir);
  717. struct inode *inode = old_dentry->d_inode;
  718. struct gfs2_inode *ip = GFS2_I(inode);
  719. struct gfs2_holder ghs[2];
  720. struct buffer_head *dibh;
  721. int alloc_required;
  722. int error;
  723. if (S_ISDIR(inode->i_mode))
  724. return -EPERM;
  725. error = gfs2_rs_alloc(dip);
  726. if (error)
  727. return error;
  728. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  729. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  730. error = gfs2_glock_nq(ghs); /* parent */
  731. if (error)
  732. goto out_parent;
  733. error = gfs2_glock_nq(ghs + 1); /* child */
  734. if (error)
  735. goto out_child;
  736. error = -ENOENT;
  737. if (inode->i_nlink == 0)
  738. goto out_gunlock;
  739. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  740. if (error)
  741. goto out_gunlock;
  742. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  743. switch (error) {
  744. case -ENOENT:
  745. break;
  746. case 0:
  747. error = -EEXIST;
  748. default:
  749. goto out_gunlock;
  750. }
  751. error = -EINVAL;
  752. if (!dip->i_inode.i_nlink)
  753. goto out_gunlock;
  754. error = -EFBIG;
  755. if (dip->i_entries == (u32)-1)
  756. goto out_gunlock;
  757. error = -EPERM;
  758. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  759. goto out_gunlock;
  760. error = -EINVAL;
  761. if (!ip->i_inode.i_nlink)
  762. goto out_gunlock;
  763. error = -EMLINK;
  764. if (ip->i_inode.i_nlink == (u32)-1)
  765. goto out_gunlock;
  766. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  767. if (error < 0)
  768. goto out_gunlock;
  769. error = 0;
  770. if (alloc_required) {
  771. error = gfs2_quota_lock_check(dip);
  772. if (error)
  773. goto out_gunlock;
  774. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres, 0);
  775. if (error)
  776. goto out_gunlock_q;
  777. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  778. gfs2_rg_blocks(dip, sdp->sd_max_dirres) +
  779. 2 * RES_DINODE + RES_STATFS +
  780. RES_QUOTA, 0);
  781. if (error)
  782. goto out_ipres;
  783. } else {
  784. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  785. if (error)
  786. goto out_ipres;
  787. }
  788. error = gfs2_meta_inode_buffer(ip, &dibh);
  789. if (error)
  790. goto out_end_trans;
  791. error = gfs2_dir_add(dir, &dentry->d_name, ip);
  792. if (error)
  793. goto out_brelse;
  794. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  795. inc_nlink(&ip->i_inode);
  796. ip->i_inode.i_ctime = CURRENT_TIME;
  797. ihold(inode);
  798. d_instantiate(dentry, inode);
  799. mark_inode_dirty(inode);
  800. out_brelse:
  801. brelse(dibh);
  802. out_end_trans:
  803. gfs2_trans_end(sdp);
  804. out_ipres:
  805. if (alloc_required)
  806. gfs2_inplace_release(dip);
  807. out_gunlock_q:
  808. if (alloc_required)
  809. gfs2_quota_unlock(dip);
  810. out_gunlock:
  811. gfs2_glock_dq(ghs + 1);
  812. out_child:
  813. gfs2_glock_dq(ghs);
  814. out_parent:
  815. gfs2_holder_uninit(ghs);
  816. gfs2_holder_uninit(ghs + 1);
  817. return error;
  818. }
  819. /*
  820. * gfs2_unlink_ok - check to see that a inode is still in a directory
  821. * @dip: the directory
  822. * @name: the name of the file
  823. * @ip: the inode
  824. *
  825. * Assumes that the lock on (at least) @dip is held.
  826. *
  827. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  828. */
  829. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  830. const struct gfs2_inode *ip)
  831. {
  832. int error;
  833. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  834. return -EPERM;
  835. if ((dip->i_inode.i_mode & S_ISVTX) &&
  836. dip->i_inode.i_uid != current_fsuid() &&
  837. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  838. return -EPERM;
  839. if (IS_APPEND(&dip->i_inode))
  840. return -EPERM;
  841. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  842. if (error)
  843. return error;
  844. error = gfs2_dir_check(&dip->i_inode, name, ip);
  845. if (error)
  846. return error;
  847. return 0;
  848. }
  849. /**
  850. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  851. * @dip: The parent directory
  852. * @name: The name of the entry in the parent directory
  853. * @inode: The inode to be removed
  854. *
  855. * Called with all the locks and in a transaction. This will only be
  856. * called for a directory after it has been checked to ensure it is empty.
  857. *
  858. * Returns: 0 on success, or an error
  859. */
  860. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  861. const struct dentry *dentry)
  862. {
  863. struct inode *inode = dentry->d_inode;
  864. struct gfs2_inode *ip = GFS2_I(inode);
  865. int error;
  866. error = gfs2_dir_del(dip, dentry);
  867. if (error)
  868. return error;
  869. ip->i_entries = 0;
  870. inode->i_ctime = CURRENT_TIME;
  871. if (S_ISDIR(inode->i_mode))
  872. clear_nlink(inode);
  873. else
  874. drop_nlink(inode);
  875. mark_inode_dirty(inode);
  876. if (inode->i_nlink == 0)
  877. gfs2_unlink_di(inode);
  878. return 0;
  879. }
  880. /**
  881. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  882. * @dir: The inode of the directory containing the inode to unlink
  883. * @dentry: The file itself
  884. *
  885. * This routine uses the type of the inode as a flag to figure out
  886. * whether this is an unlink or an rmdir.
  887. *
  888. * Returns: errno
  889. */
  890. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  891. {
  892. struct gfs2_inode *dip = GFS2_I(dir);
  893. struct gfs2_sbd *sdp = GFS2_SB(dir);
  894. struct inode *inode = dentry->d_inode;
  895. struct gfs2_inode *ip = GFS2_I(inode);
  896. struct gfs2_holder ghs[3];
  897. struct gfs2_rgrpd *rgd;
  898. int error;
  899. error = gfs2_rindex_update(sdp);
  900. if (error)
  901. return error;
  902. error = -EROFS;
  903. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  904. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  905. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  906. if (!rgd)
  907. goto out_inodes;
  908. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  909. error = gfs2_glock_nq(ghs); /* parent */
  910. if (error)
  911. goto out_parent;
  912. error = gfs2_glock_nq(ghs + 1); /* child */
  913. if (error)
  914. goto out_child;
  915. error = -ENOENT;
  916. if (inode->i_nlink == 0)
  917. goto out_rgrp;
  918. if (S_ISDIR(inode->i_mode)) {
  919. error = -ENOTEMPTY;
  920. if (ip->i_entries > 2 || inode->i_nlink > 2)
  921. goto out_rgrp;
  922. }
  923. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  924. if (error)
  925. goto out_rgrp;
  926. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  927. if (error)
  928. goto out_gunlock;
  929. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  930. if (error)
  931. goto out_end_trans;
  932. error = gfs2_unlink_inode(dip, dentry);
  933. out_end_trans:
  934. gfs2_trans_end(sdp);
  935. out_gunlock:
  936. gfs2_glock_dq(ghs + 2);
  937. out_rgrp:
  938. gfs2_glock_dq(ghs + 1);
  939. out_child:
  940. gfs2_glock_dq(ghs);
  941. out_parent:
  942. gfs2_holder_uninit(ghs + 2);
  943. out_inodes:
  944. gfs2_holder_uninit(ghs + 1);
  945. gfs2_holder_uninit(ghs);
  946. return error;
  947. }
  948. /**
  949. * gfs2_symlink - Create a symlink
  950. * @dir: The directory to create the symlink in
  951. * @dentry: The dentry to put the symlink in
  952. * @symname: The thing which the link points to
  953. *
  954. * Returns: errno
  955. */
  956. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  957. const char *symname)
  958. {
  959. struct gfs2_sbd *sdp = GFS2_SB(dir);
  960. unsigned int size;
  961. size = strlen(symname);
  962. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  963. return -ENAMETOOLONG;
  964. return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
  965. }
  966. /**
  967. * gfs2_mkdir - Make a directory
  968. * @dir: The parent directory of the new one
  969. * @dentry: The dentry of the new directory
  970. * @mode: The mode of the new directory
  971. *
  972. * Returns: errno
  973. */
  974. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  975. {
  976. return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, 0, 0);
  977. }
  978. /**
  979. * gfs2_mknod - Make a special file
  980. * @dir: The directory in which the special file will reside
  981. * @dentry: The dentry of the special file
  982. * @mode: The mode of the special file
  983. * @dev: The device specification of the special file
  984. *
  985. */
  986. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  987. dev_t dev)
  988. {
  989. return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0, 0);
  990. }
  991. /*
  992. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  993. * @this: move this
  994. * @to: to here
  995. *
  996. * Follow @to back to the root and make sure we don't encounter @this
  997. * Assumes we already hold the rename lock.
  998. *
  999. * Returns: errno
  1000. */
  1001. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  1002. {
  1003. struct inode *dir = &to->i_inode;
  1004. struct super_block *sb = dir->i_sb;
  1005. struct inode *tmp;
  1006. int error = 0;
  1007. igrab(dir);
  1008. for (;;) {
  1009. if (dir == &this->i_inode) {
  1010. error = -EINVAL;
  1011. break;
  1012. }
  1013. if (dir == sb->s_root->d_inode) {
  1014. error = 0;
  1015. break;
  1016. }
  1017. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1018. if (IS_ERR(tmp)) {
  1019. error = PTR_ERR(tmp);
  1020. break;
  1021. }
  1022. iput(dir);
  1023. dir = tmp;
  1024. }
  1025. iput(dir);
  1026. return error;
  1027. }
  1028. /**
  1029. * gfs2_rename - Rename a file
  1030. * @odir: Parent directory of old file name
  1031. * @odentry: The old dentry of the file
  1032. * @ndir: Parent directory of new file name
  1033. * @ndentry: The new dentry of the file
  1034. *
  1035. * Returns: errno
  1036. */
  1037. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1038. struct inode *ndir, struct dentry *ndentry)
  1039. {
  1040. struct gfs2_inode *odip = GFS2_I(odir);
  1041. struct gfs2_inode *ndip = GFS2_I(ndir);
  1042. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  1043. struct gfs2_inode *nip = NULL;
  1044. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1045. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  1046. struct gfs2_rgrpd *nrgd;
  1047. unsigned int num_gh;
  1048. int dir_rename = 0;
  1049. int alloc_required = 0;
  1050. unsigned int x;
  1051. int error;
  1052. if (ndentry->d_inode) {
  1053. nip = GFS2_I(ndentry->d_inode);
  1054. if (ip == nip)
  1055. return 0;
  1056. }
  1057. error = gfs2_rindex_update(sdp);
  1058. if (error)
  1059. return error;
  1060. error = gfs2_rs_alloc(ndip);
  1061. if (error)
  1062. return error;
  1063. if (odip != ndip) {
  1064. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1065. 0, &r_gh);
  1066. if (error)
  1067. goto out;
  1068. if (S_ISDIR(ip->i_inode.i_mode)) {
  1069. dir_rename = 1;
  1070. /* don't move a dirctory into it's subdir */
  1071. error = gfs2_ok_to_move(ip, ndip);
  1072. if (error)
  1073. goto out_gunlock_r;
  1074. }
  1075. }
  1076. num_gh = 1;
  1077. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1078. if (odip != ndip) {
  1079. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1080. num_gh++;
  1081. }
  1082. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1083. num_gh++;
  1084. if (nip) {
  1085. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1086. num_gh++;
  1087. /* grab the resource lock for unlink flag twiddling
  1088. * this is the case of the target file already existing
  1089. * so we unlink before doing the rename
  1090. */
  1091. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
  1092. if (nrgd)
  1093. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1094. }
  1095. for (x = 0; x < num_gh; x++) {
  1096. error = gfs2_glock_nq(ghs + x);
  1097. if (error)
  1098. goto out_gunlock;
  1099. }
  1100. error = -ENOENT;
  1101. if (ip->i_inode.i_nlink == 0)
  1102. goto out_gunlock;
  1103. /* Check out the old directory */
  1104. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1105. if (error)
  1106. goto out_gunlock;
  1107. /* Check out the new directory */
  1108. if (nip) {
  1109. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1110. if (error)
  1111. goto out_gunlock;
  1112. if (nip->i_inode.i_nlink == 0) {
  1113. error = -EAGAIN;
  1114. goto out_gunlock;
  1115. }
  1116. if (S_ISDIR(nip->i_inode.i_mode)) {
  1117. if (nip->i_entries < 2) {
  1118. gfs2_consist_inode(nip);
  1119. error = -EIO;
  1120. goto out_gunlock;
  1121. }
  1122. if (nip->i_entries > 2) {
  1123. error = -ENOTEMPTY;
  1124. goto out_gunlock;
  1125. }
  1126. }
  1127. } else {
  1128. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1129. if (error)
  1130. goto out_gunlock;
  1131. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1132. switch (error) {
  1133. case -ENOENT:
  1134. error = 0;
  1135. break;
  1136. case 0:
  1137. error = -EEXIST;
  1138. default:
  1139. goto out_gunlock;
  1140. };
  1141. if (odip != ndip) {
  1142. if (!ndip->i_inode.i_nlink) {
  1143. error = -ENOENT;
  1144. goto out_gunlock;
  1145. }
  1146. if (ndip->i_entries == (u32)-1) {
  1147. error = -EFBIG;
  1148. goto out_gunlock;
  1149. }
  1150. if (S_ISDIR(ip->i_inode.i_mode) &&
  1151. ndip->i_inode.i_nlink == (u32)-1) {
  1152. error = -EMLINK;
  1153. goto out_gunlock;
  1154. }
  1155. }
  1156. }
  1157. /* Check out the dir to be renamed */
  1158. if (dir_rename) {
  1159. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1160. if (error)
  1161. goto out_gunlock;
  1162. }
  1163. if (nip == NULL)
  1164. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  1165. error = alloc_required;
  1166. if (error < 0)
  1167. goto out_gunlock;
  1168. if (alloc_required) {
  1169. error = gfs2_quota_lock_check(ndip);
  1170. if (error)
  1171. goto out_gunlock;
  1172. error = gfs2_inplace_reserve(ndip, sdp->sd_max_dirres, 0);
  1173. if (error)
  1174. goto out_gunlock_q;
  1175. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  1176. gfs2_rg_blocks(ndip, sdp->sd_max_dirres) +
  1177. 4 * RES_DINODE + 4 * RES_LEAF +
  1178. RES_STATFS + RES_QUOTA + 4, 0);
  1179. if (error)
  1180. goto out_ipreserv;
  1181. } else {
  1182. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1183. 5 * RES_LEAF + 4, 0);
  1184. if (error)
  1185. goto out_gunlock;
  1186. }
  1187. /* Remove the target file, if it exists */
  1188. if (nip)
  1189. error = gfs2_unlink_inode(ndip, ndentry);
  1190. if (dir_rename) {
  1191. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1192. if (error)
  1193. goto out_end_trans;
  1194. } else {
  1195. struct buffer_head *dibh;
  1196. error = gfs2_meta_inode_buffer(ip, &dibh);
  1197. if (error)
  1198. goto out_end_trans;
  1199. ip->i_inode.i_ctime = CURRENT_TIME;
  1200. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1201. gfs2_dinode_out(ip, dibh->b_data);
  1202. brelse(dibh);
  1203. }
  1204. error = gfs2_dir_del(odip, odentry);
  1205. if (error)
  1206. goto out_end_trans;
  1207. error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
  1208. if (error)
  1209. goto out_end_trans;
  1210. out_end_trans:
  1211. gfs2_trans_end(sdp);
  1212. out_ipreserv:
  1213. if (alloc_required)
  1214. gfs2_inplace_release(ndip);
  1215. out_gunlock_q:
  1216. if (alloc_required)
  1217. gfs2_quota_unlock(ndip);
  1218. out_gunlock:
  1219. while (x--) {
  1220. gfs2_glock_dq(ghs + x);
  1221. gfs2_holder_uninit(ghs + x);
  1222. }
  1223. out_gunlock_r:
  1224. if (r_gh.gh_gl)
  1225. gfs2_glock_dq_uninit(&r_gh);
  1226. out:
  1227. return error;
  1228. }
  1229. /**
  1230. * gfs2_follow_link - Follow a symbolic link
  1231. * @dentry: The dentry of the link
  1232. * @nd: Data that we pass to vfs_follow_link()
  1233. *
  1234. * This can handle symlinks of any size.
  1235. *
  1236. * Returns: 0 on success or error code
  1237. */
  1238. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1239. {
  1240. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1241. struct gfs2_holder i_gh;
  1242. struct buffer_head *dibh;
  1243. unsigned int size;
  1244. char *buf;
  1245. int error;
  1246. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1247. error = gfs2_glock_nq(&i_gh);
  1248. if (error) {
  1249. gfs2_holder_uninit(&i_gh);
  1250. nd_set_link(nd, ERR_PTR(error));
  1251. return NULL;
  1252. }
  1253. size = (unsigned int)i_size_read(&ip->i_inode);
  1254. if (size == 0) {
  1255. gfs2_consist_inode(ip);
  1256. buf = ERR_PTR(-EIO);
  1257. goto out;
  1258. }
  1259. error = gfs2_meta_inode_buffer(ip, &dibh);
  1260. if (error) {
  1261. buf = ERR_PTR(error);
  1262. goto out;
  1263. }
  1264. buf = kzalloc(size + 1, GFP_NOFS);
  1265. if (!buf)
  1266. buf = ERR_PTR(-ENOMEM);
  1267. else
  1268. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1269. brelse(dibh);
  1270. out:
  1271. gfs2_glock_dq_uninit(&i_gh);
  1272. nd_set_link(nd, buf);
  1273. return NULL;
  1274. }
  1275. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1276. {
  1277. char *s = nd_get_link(nd);
  1278. if (!IS_ERR(s))
  1279. kfree(s);
  1280. }
  1281. /**
  1282. * gfs2_permission -
  1283. * @inode: The inode
  1284. * @mask: The mask to be tested
  1285. * @flags: Indicates whether this is an RCU path walk or not
  1286. *
  1287. * This may be called from the VFS directly, or from within GFS2 with the
  1288. * inode locked, so we look to see if the glock is already locked and only
  1289. * lock the glock if its not already been done.
  1290. *
  1291. * Returns: errno
  1292. */
  1293. int gfs2_permission(struct inode *inode, int mask)
  1294. {
  1295. struct gfs2_inode *ip;
  1296. struct gfs2_holder i_gh;
  1297. int error;
  1298. int unlock = 0;
  1299. ip = GFS2_I(inode);
  1300. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1301. if (mask & MAY_NOT_BLOCK)
  1302. return -ECHILD;
  1303. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1304. if (error)
  1305. return error;
  1306. unlock = 1;
  1307. }
  1308. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1309. error = -EACCES;
  1310. else
  1311. error = generic_permission(inode, mask);
  1312. if (unlock)
  1313. gfs2_glock_dq_uninit(&i_gh);
  1314. return error;
  1315. }
  1316. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1317. {
  1318. setattr_copy(inode, attr);
  1319. mark_inode_dirty(inode);
  1320. return 0;
  1321. }
  1322. /**
  1323. * gfs2_setattr_simple -
  1324. * @ip:
  1325. * @attr:
  1326. *
  1327. * Returns: errno
  1328. */
  1329. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1330. {
  1331. int error;
  1332. if (current->journal_info)
  1333. return __gfs2_setattr_simple(inode, attr);
  1334. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1335. if (error)
  1336. return error;
  1337. error = __gfs2_setattr_simple(inode, attr);
  1338. gfs2_trans_end(GFS2_SB(inode));
  1339. return error;
  1340. }
  1341. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1342. {
  1343. struct gfs2_inode *ip = GFS2_I(inode);
  1344. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1345. u32 ouid, ogid, nuid, ngid;
  1346. int error;
  1347. ouid = inode->i_uid;
  1348. ogid = inode->i_gid;
  1349. nuid = attr->ia_uid;
  1350. ngid = attr->ia_gid;
  1351. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  1352. ouid = nuid = NO_QUOTA_CHANGE;
  1353. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  1354. ogid = ngid = NO_QUOTA_CHANGE;
  1355. error = gfs2_quota_lock(ip, nuid, ngid);
  1356. if (error)
  1357. return error;
  1358. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1359. error = gfs2_quota_check(ip, nuid, ngid);
  1360. if (error)
  1361. goto out_gunlock_q;
  1362. }
  1363. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1364. if (error)
  1365. goto out_gunlock_q;
  1366. error = gfs2_setattr_simple(inode, attr);
  1367. if (error)
  1368. goto out_end_trans;
  1369. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1370. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1371. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1372. gfs2_quota_change(ip, blocks, nuid, ngid);
  1373. }
  1374. out_end_trans:
  1375. gfs2_trans_end(sdp);
  1376. out_gunlock_q:
  1377. gfs2_quota_unlock(ip);
  1378. return error;
  1379. }
  1380. /**
  1381. * gfs2_setattr - Change attributes on an inode
  1382. * @dentry: The dentry which is changing
  1383. * @attr: The structure describing the change
  1384. *
  1385. * The VFS layer wants to change one or more of an inodes attributes. Write
  1386. * that change out to disk.
  1387. *
  1388. * Returns: errno
  1389. */
  1390. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1391. {
  1392. struct inode *inode = dentry->d_inode;
  1393. struct gfs2_inode *ip = GFS2_I(inode);
  1394. struct gfs2_holder i_gh;
  1395. int error;
  1396. error = gfs2_rs_alloc(ip);
  1397. if (error)
  1398. return error;
  1399. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1400. if (error)
  1401. return error;
  1402. error = -EPERM;
  1403. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1404. goto out;
  1405. error = inode_change_ok(inode, attr);
  1406. if (error)
  1407. goto out;
  1408. if (attr->ia_valid & ATTR_SIZE)
  1409. error = gfs2_setattr_size(inode, attr->ia_size);
  1410. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1411. error = setattr_chown(inode, attr);
  1412. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1413. error = gfs2_acl_chmod(ip, attr);
  1414. else
  1415. error = gfs2_setattr_simple(inode, attr);
  1416. out:
  1417. if (!error)
  1418. mark_inode_dirty(inode);
  1419. gfs2_glock_dq_uninit(&i_gh);
  1420. return error;
  1421. }
  1422. /**
  1423. * gfs2_getattr - Read out an inode's attributes
  1424. * @mnt: The vfsmount the inode is being accessed from
  1425. * @dentry: The dentry to stat
  1426. * @stat: The inode's stats
  1427. *
  1428. * This may be called from the VFS directly, or from within GFS2 with the
  1429. * inode locked, so we look to see if the glock is already locked and only
  1430. * lock the glock if its not already been done. Note that its the NFS
  1431. * readdirplus operation which causes this to be called (from filldir)
  1432. * with the glock already held.
  1433. *
  1434. * Returns: errno
  1435. */
  1436. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1437. struct kstat *stat)
  1438. {
  1439. struct inode *inode = dentry->d_inode;
  1440. struct gfs2_inode *ip = GFS2_I(inode);
  1441. struct gfs2_holder gh;
  1442. int error;
  1443. int unlock = 0;
  1444. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1445. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1446. if (error)
  1447. return error;
  1448. unlock = 1;
  1449. }
  1450. generic_fillattr(inode, stat);
  1451. if (unlock)
  1452. gfs2_glock_dq_uninit(&gh);
  1453. return 0;
  1454. }
  1455. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1456. const void *data, size_t size, int flags)
  1457. {
  1458. struct inode *inode = dentry->d_inode;
  1459. struct gfs2_inode *ip = GFS2_I(inode);
  1460. struct gfs2_holder gh;
  1461. int ret;
  1462. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1463. ret = gfs2_glock_nq(&gh);
  1464. if (ret == 0) {
  1465. ret = gfs2_rs_alloc(ip);
  1466. if (ret == 0)
  1467. ret = generic_setxattr(dentry, name, data, size, flags);
  1468. gfs2_glock_dq(&gh);
  1469. }
  1470. gfs2_holder_uninit(&gh);
  1471. return ret;
  1472. }
  1473. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1474. void *data, size_t size)
  1475. {
  1476. struct inode *inode = dentry->d_inode;
  1477. struct gfs2_inode *ip = GFS2_I(inode);
  1478. struct gfs2_holder gh;
  1479. int ret;
  1480. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1481. ret = gfs2_glock_nq(&gh);
  1482. if (ret == 0) {
  1483. ret = generic_getxattr(dentry, name, data, size);
  1484. gfs2_glock_dq(&gh);
  1485. }
  1486. gfs2_holder_uninit(&gh);
  1487. return ret;
  1488. }
  1489. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1490. {
  1491. struct inode *inode = dentry->d_inode;
  1492. struct gfs2_inode *ip = GFS2_I(inode);
  1493. struct gfs2_holder gh;
  1494. int ret;
  1495. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1496. ret = gfs2_glock_nq(&gh);
  1497. if (ret == 0) {
  1498. ret = gfs2_rs_alloc(ip);
  1499. if (ret == 0)
  1500. ret = generic_removexattr(dentry, name);
  1501. gfs2_glock_dq(&gh);
  1502. }
  1503. gfs2_holder_uninit(&gh);
  1504. return ret;
  1505. }
  1506. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1507. u64 start, u64 len)
  1508. {
  1509. struct gfs2_inode *ip = GFS2_I(inode);
  1510. struct gfs2_holder gh;
  1511. int ret;
  1512. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1513. if (ret)
  1514. return ret;
  1515. mutex_lock(&inode->i_mutex);
  1516. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1517. if (ret)
  1518. goto out;
  1519. if (gfs2_is_stuffed(ip)) {
  1520. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1521. u64 size = i_size_read(inode);
  1522. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1523. FIEMAP_EXTENT_DATA_INLINE;
  1524. phys += sizeof(struct gfs2_dinode);
  1525. phys += start;
  1526. if (start + len > size)
  1527. len = size - start;
  1528. if (start < size)
  1529. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1530. len, flags);
  1531. if (ret == 1)
  1532. ret = 0;
  1533. } else {
  1534. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1535. gfs2_block_map);
  1536. }
  1537. gfs2_glock_dq_uninit(&gh);
  1538. out:
  1539. mutex_unlock(&inode->i_mutex);
  1540. return ret;
  1541. }
  1542. const struct inode_operations gfs2_file_iops = {
  1543. .permission = gfs2_permission,
  1544. .setattr = gfs2_setattr,
  1545. .getattr = gfs2_getattr,
  1546. .setxattr = gfs2_setxattr,
  1547. .getxattr = gfs2_getxattr,
  1548. .listxattr = gfs2_listxattr,
  1549. .removexattr = gfs2_removexattr,
  1550. .fiemap = gfs2_fiemap,
  1551. .get_acl = gfs2_get_acl,
  1552. };
  1553. const struct inode_operations gfs2_dir_iops = {
  1554. .create = gfs2_create,
  1555. .lookup = gfs2_lookup,
  1556. .link = gfs2_link,
  1557. .unlink = gfs2_unlink,
  1558. .symlink = gfs2_symlink,
  1559. .mkdir = gfs2_mkdir,
  1560. .rmdir = gfs2_unlink,
  1561. .mknod = gfs2_mknod,
  1562. .rename = gfs2_rename,
  1563. .permission = gfs2_permission,
  1564. .setattr = gfs2_setattr,
  1565. .getattr = gfs2_getattr,
  1566. .setxattr = gfs2_setxattr,
  1567. .getxattr = gfs2_getxattr,
  1568. .listxattr = gfs2_listxattr,
  1569. .removexattr = gfs2_removexattr,
  1570. .fiemap = gfs2_fiemap,
  1571. .get_acl = gfs2_get_acl,
  1572. };
  1573. const struct inode_operations gfs2_symlink_iops = {
  1574. .readlink = generic_readlink,
  1575. .follow_link = gfs2_follow_link,
  1576. .put_link = gfs2_put_link,
  1577. .permission = gfs2_permission,
  1578. .setattr = gfs2_setattr,
  1579. .getattr = gfs2_getattr,
  1580. .setxattr = gfs2_setxattr,
  1581. .getxattr = gfs2_getxattr,
  1582. .listxattr = gfs2_listxattr,
  1583. .removexattr = gfs2_removexattr,
  1584. .fiemap = gfs2_fiemap,
  1585. .get_acl = gfs2_get_acl,
  1586. };