inode.c 43 KB

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